MySQL数据库线程缓冲池详解


  本文标签:MySQL 线程缓冲池

  MySQL数据库线程缓冲池的相关知识是本文我们主要要介绍的内容,MySQL数据库支持线程缓存,在多线程连接模式下,如果连接断开后,将这个线程放入空闲线程缓冲区,在下次有连接到来时,先去缓冲池中查找是否有空闲线程,有则用之,无则创建  。启动时可以设置线程缓冲池的数目:Mysqld.exe --thread_cache_size=10  。

  在一个连接断开时,会调用cache_thread函数,将空闲的线程加入到cache中,以备后用  。源码如下:

  

  1. static bool cache_thread()  
  2. {  
  3. safe_mutex_assert_owner(&LOCK_thread_count);  
  4. if (  
  5. cached_thread_count < thread_cache_size 
  6. &&  
  7. ! abort_loop && !kill_cached_threads)  
  8. {  
  9. /* Dont kill the thread, just put it in cache for reuse */  
  10. DBUG_PRINT("info", ("Adding thread to cache"));  
  11. cached_thread_count++;  
  12. while (!abort_loop && ! wake_thread && ! kill_cached_threads)  
  13. (void) pthread_cond_wait(&COND_thread_cache, &LOCK_thread_count);  
  14. cached_thread_count--;  
  15. if (kill_cached_threads)  
  16. pthread_cond_signal(&COND_flush_thread_cache);  
  17. if (wake_thread)  
  18. {  
  19. THD *thd;  
  20. wake_thread--;  
  21. thdthread_cache.get();  
  22. thd->thread_stack= (char*) &thd;          // For store_globals  
  23. (void) thd->store_globals();  
  24. /*  
  25. THD::mysys_var::abort is associated with physical thread rather  
  26. than with THD object. So we need to reset this flag before using  
  27. this thread for handling of new THD object/connection.  
  28. */  
  29. thd->mysys_var->abort0;  
  30. thd->thr_create_utimemy_micro_time();  
  31. threads.append(thd);  
  32. return(1);  
  33. }  
  34. }  
  35. return(0);  

  

  上面我们的启动参数设置线程缓冲区为10,此时对应代码里面的thread_cache_size = 10,cached_thread_count记录

  了此刻cache中的空闲线程数目,只有在cache未满的情况下,才会将新的空闲线程加入缓冲池中  。加入到缓冲区其实就是将线

  程挂起,pthread_cond_wait函数便是线程等待函数,在此函数中,会调用WaitForMultipleObjects进行事件等待  。具体源码

  如下:

  

  1. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,  
  2. struct timespec *abstime)  
  3. int result;  
  4. long timeout;   
  5. union ft64 now;  
  6. if( abstime != NULL )  
  7. {  
  8. GetSystemTimeAsFileTime(&now.ft);  
  9. /*  
  10. Calculate time left to abstime  
  11. - subtract start time from current time(values are in 100ns units)  
  12. - convert to millisec by dividing with 10000  
  13. */  
  14. timeout= (long)((abstime->tv.i64 - now.i64) / 10000);  
  15. /* Dont allow the timeout to be negative */  
  16. if (timeout < 0)  
  17. timeout0L;  
  18. /*  
  19. Make sure the calucated timeout does not exceed original timeout  
  20. value which could cause "wait for ever" if system time changes  
  21. */  
  22. if (timeout > abstime->max_timeout_msec)  
  23. timeoutabstime->max_timeout_msec;  
  24. }  
  25. else  
  26. {  
  27. /* No time specified; dont expire */  
  28. timeoutINFINITE;  
  29. }  
  30. /*   
  31. Block access if previous broadcast hasnt finished.  
  32. This is just for safety and should normally not  
  33. affect the total time spent in this function.  
  34. */  
  35. WaitForSingleObject(cond->broadcast_block_event, INFINITE);  
  36. EnterCriticalSection(&cond->lock_waiting);  
  37. cond->waiting++;  
  38. LeaveCriticalSection(&cond->lock_waiting);  
  39. LeaveCriticalSection(mutex);  
  40. resultWaitForMultipleObjects(2, cond->events, FALSE, timeout);  
  41. EnterCriticalSection(&cond->lock_waiting);  
  42. cond->waiting--;  
  43. if (cond->waiting == 0)  
  44. {  
  45. /*  
  46. Were the last waiter to be notified or to stop waiting, so  
  47. reset the manual event.   
  48. */  
  49. /* Close broadcast gate */  
  50. ResetEvent(cond->events[BROADCAST]);  
  51. /* Open block gate */  
  52. SetEvent(cond->broadcast_block_event);  
  53. }  
  54. LeaveCriticalSection(&cond->lock_waiting);  
  55. EnterCriticalSection(mutex);  
  56. return result == WAIT_TIMEOUT ? ETIMEDOUT : 0;  

  

  此处是等待时间,何处进行事件通知呢?我们再次来到上篇所提及的为新的连接创建线程的代码中:

  

  1. void create_thread_to_handle_connection(THD *thd)  
  2. {  
  3. if (cached_thread_count > wake_thread)  
  4. {  
  5. /* Get thread from cache */  
  6. thread_cache.append(thd);  
  7. wake_thread++;  
  8. pthread_cond_signal(&COND_thread_cache);  
  9. }  
  10. Else  
  11. ...  

  

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