成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

MySQL數據庫線程緩沖池詳解

數據庫 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. /* Don't 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. /* Don't 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; don't expire */  
  28. timeoutINFINITE;  
  29. }  
  30. /*   
  31. Block access if previous broadcast hasn't 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. We're 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數據庫線程緩沖池的相關知識就介紹到這里了,希望本次的介紹能夠對您有所收獲!

【編輯推薦】

  1. 如何檢查MySQL數據庫的主從延時?
  2. MySQL數據庫時間類型的物理存儲
  3. Linux cron執行MySQL失敗的問題解決方案
  4. PHP與MySQL數據庫中排序的對比及使用條件詳解
  5. MySQL性能優化之使用Limit關鍵字來避免全表掃描
責任編輯:趙鵬 來源: 博客園
相關推薦

2019-09-29 17:40:55

緩沖池MySQL數據庫

2011-08-10 11:07:34

MySQL查詢緩沖

2011-08-30 13:40:28

MySQL線程

2010-03-29 10:19:24

2022-03-29 10:52:08

MySQL數據庫

2009-06-16 09:25:31

JBoss配置

2017-06-22 14:13:07

PythonMySQLpymysqlpool

2010-05-19 13:37:39

MySQL數據庫密碼

2024-11-21 07:00:00

線程池Java開發

2022-03-22 15:05:15

MySQL緩沖池

2009-06-15 13:46:00

netbeans設置數據庫連接池

2018-07-30 15:00:05

數據庫MySQLJOIN

2009-12-31 11:10:01

2009-09-07 15:25:24

MySQL數據庫互操作Silverlight

2009-06-24 07:53:47

Hibernate數據

2025-01-14 00:10:00

Java應用程序

2011-07-05 10:03:00

Qt MYSQL 數據庫

2017-07-12 09:20:42

SQLite數據庫移植

2017-04-01 18:30:47

MySQL誤刪除數據庫

2011-08-09 15:25:14

線程池數據庫連接池
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产伦精品一区二区三区视频金莲 | 日韩视频在线免费观看 | 亚洲一区| 欧美日韩a| 男女免费在线观看视频 | 成人精品一区二区 | 精品国产乱码久久久久久蜜柚 | 久久91| 亚洲网站在线观看 | 精品欧美乱码久久久久久1区2区 | 激情91 | 日本午夜在线视频 | 99视频免费在线观看 | 影音先锋中文在线 | 久久综合久色欧美综合狠狠 | 日本在线中文 | 久久福利网站 | 免费不卡av| 欧美日韩一区二区三区四区 | 日本a视频 | 亚洲a在线视频 | 午夜精品久久久久久久99黑人 | 影音先锋成人资源 | 午夜影院在线观看 | 97色综合| 成人免费淫片aa视频免费 | 久久se精品一区精品二区 | 国产精品久久久久久久久久了 | 羞羞网站免费观看 | 亚洲一区网站 | 涩涩导航 | 欧美在线视频一区二区 | 久久国产精品99久久久久久丝袜 | 国产精品一区二区av | av性色| 91人人看 | 涩涩99| 青青草这里只有精品 | 日本大片在线播放 | 久久久久久国产 | 久久99精品久久久久久噜噜 |