MySQL数据库创建线程的相关操作详解


  本文标签:MySQL 线程

  MySQL数据库创建线程的相关操作是本文我们主要要介绍的内容,MySQL数据库中,为了提高系统效率,减少频繁创建线程和中止线程的系统消耗,MySQL使用了线程缓冲区的概念,即如果一个连接断开,则并不销毁承载其的线程,而是将此线程放入线程缓冲区,并处于挂起状态,当下一个新的Connection到来时,首先去线程缓冲区去查找是否有空闲的线程,如果有,则使用之,如果没有则新建线程  。

  1.线程创建函数

  大家知道,Mysql现在是插件式的存储引擎,只要实现规定的接口,就可实现自己的存储引擎  。故Mysql的线程创建除了出现在主服务器框架外,存储引擎也可能会进行线程的创建  。通过设置断点,在我调试的版本中,发现了两个创建线程的函数  。

  pthread_create:Mysql自用的创建线程函数

  os_thread_create:存储引擎innobase的创建线程的函数

  os_thread_create是存储引擎innobase的线程函数,先搁浅不研究了,重点看下pthread_create,首先看下其源码  。

  1. int pthread_create(pthread_t *thread_id, pthread_attr_t *attr,  
  2. pthread_handler func, void *param)  
  3. {  
  4. HANDLE hThread;  
  5. struct pthread_map *map;  
  6. DBUG_ENTER("pthread_create");  
  7. if (!(map=malloc(sizeof(*map))))  
  8. DBUG_RETURN(-1);  
  9. map->funcfunc=func; map->paramparam=param;  
  10. pthread_mutex_lock(&THR_LOCK_thread);  
  11. #ifdef __BORLANDC__  
  12. hThread=(HANDLE)_beginthread((void(_USERENTRY *)(void *)) pthread_start,  
  13. attr->dwStackSize ? attr->dwStackSize :  
  14. 65535, (void*) map);  
  15. #else  
  16. hThread=(HANDLE)_beginthread((void( __cdecl *)(void *)) pthread_start, attr->dwStackSize ? attr->dwStackSize : 65535, (void*) map);  
  17. #endif  
  18. DBUG_PRINT("info", ("hThread=%lu",(long) hThread));  
  19. *thread_id=map->pthreadself=hThread;  
  20. pthread_mutex_unlock(&THR_LOCK_thread);  
  21. if (hThread == (HANDLE) -1)  
  22. {  
  23. int error=errno;  
  24. DBUG_PRINT("error",  
  25. ("Cant create thread to handle request (error %d)",error));  
  26. DBUG_RETURN(error ? error : -1);  
  27. }  
  28. VOID(SetThreadPriority(hThread, attr->priority)) ;  
  29. DBUG_RETURN(0);  

  上面代码首先构造了一个map结构体,成员分别是函数地址和传入参数  。然后调用操作系统的接口,_beginthread,但是执行函数并不是传入的函数——func,而是pthread_start,参数为map  。继续跟踪pthread_start  。

  1. pthread_handler_t pthread_start(void *param)  
  2. {  
  3. pthread_handler  
  4. func=((struct pthread_map *) param)->func  
  5. void *func_param=((struct pthread_map *) param)->param;  
  6. my_thread_init();         /* Will always succeed in windows */  
  7. pthread_mutex_lock(&THR_LOCK_thread);   /* Wait for beginthread to return */  
  8. win_pthread_self=((struct pthread_map *) param)->pthreadself;  
  9. pthread_mutex_unlock(&THR_LOCK_thread);  
  10. free((char*) param);            /* Free param from create */  
  11. pthread_exit((void*) (*func)(func_param));  
  12. return 0;               /* Safety */  

  可以看出,pthread_start中调用了map的func元素,作为真正执行的函数体  。OK,创建线程的函数跟踪到此!

  2.服务器启动时创建了哪些函数?

  通过在两个创建线程的地方设置断点,总结了下,在服务器启动时,创建了如下的线程  。

  pthread_create创建的线程:

  
创建线程函数 线程执行函数

  create_shutdown_thread

  handle_shutdown

  start_handle_manager

  handle_manager

  handle_connections_methods

  handle_connections_sockets

  innobase的os_thread_create创建的线程:

  
创建线程函数 线程执行函数

  innobase_start_or_create_for_mysql

  io_handler_thread(4个)

  recv_recovery_from_checkpoint_finish

  trx_rollback_or_clean_all_without_sess

  innobase_start_or_create_for_mysql

  srv_lock_timeout_thread

 

  srv_error_monitor_thread

 

  srv_monitor_thread

 

  srv_master_thread

  还可以在调试过程中,通过暂停来看此时服务器中的线程,如下图:

  MySQL数据库创建线程的相关操作详解

  关于MySQL数据库创建线程的相关知识就介绍到这里了,希望本次的介绍能够对您有所收获!