The MySQL database thread buffer pool Knowledge is this article we mainly want to introduce the content, MySQL database support thread caching, in multithreaded connection mode, if the connection is disconnected, put this thread into the free thread buffer, the next time there is a connection, go to the buffer pool to find whether there are idle threads, there are , none is created. The number of thread buffer pools can be set at startup: Mysqld.exe--thread_cache_size=10.
When a connection is disconnected, the Cache_thread function is called and the idle thread is added to the cache for later use. The source code is as follows:
The
static bool Cache_thread () { Safe_mutex_assert_owner (&lock_thread_count); if ( Cached_thread_count < thread_cache_size && ! Abort_loop &&!kill_cached_thread s) { /* Don T kill the thread, just put it in cache for reuse */ Dbug_print ("Info", ("adding thread to Cach E ")); cached_thread_count++; while (!abort_loop &&! Wake_thread &&! kill_cached_threads) (void) pthread_cond_wait (&con D_thread_cache, &lock_thread_count); cached_thread_count--; if (kill_cached_threads) Pthread_cond_signal (&cond_flush_thread_cache); if (wake_thread) { THD *thd; wake_thread--; thd= thread_cache.get (); thd->thread_stack= (char*) &thd;//For store_globals (void) thd->store_globals (); /* Thd::mysys_var::abort is associated with physical thrEAD rather than with THD object. So we need to reset this flag before the using this thread for handling of new THD object/connection. */ thd->mysys_var->abort= 0; Thd->thr_create_utime= my_micro_time (); Threads.append (THD); return (1); } } return (0); } |
Above our startup parameter set the thread buffer to 10, at which point the Thread_cache_size = 10,cached_thread_count record in the corresponding code
The number of idle threads in the cache, the new idle thread is added to the buffer pool only if the cache is not full. Adding to the buffer is actually the line
Process hangs, the pthread_cond_wait function is the thread wait function, in which the waitformultipleobjects is invoked to wait for the event. Specific source
As follows:
The following code fragment:
int pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex,
struct Timespec *abstime)
int result;
long timeout;
Union Ft64 now;
if (abstime!= NULL)
{
Getsystemtimeasfiletime (&now.ft);
/*
Calculate Time left to Abstime
-Subtract start time from the current time (values are in 100ns units)
-Convert to millisec by dividing with 10000
*/
timeout= (Long) (abstime->tv.i64-now.i64)/10000);
/* Don ' t allow the timeout to be negative * *
if (Timeout < 0)
timeout= 0L;
/*
Make sure the calucated timeout does not exceed original timeout
value which could cause ' wait for ever ' if system time changes
*/
if (Timeout > abstime->max_timeout_msec)
timeout= abstime->max_timeout_msec;
}
Else
{
/* No time specified; Don ' t expire * *
timeout= INFINITE;
}
/*
block access If previous broadcast hasn ' t finished.
This is just to safety and should normally not
affect the total time spent into this function.
*/
WaitForSingleObject (cond->broadcast_block_event, INFINITE);
entercriticalsection (&cond->lock_waiting);
cond->waiting++;
leavecriticalsection (&cond->lock_waiting);
leavecriticalsection (mutex);
result= waitformultipleobjects (2, cond->events, FALSE, timeout);
entercriticalsection (&cond->lock_waiting);
cond->waiting--;
if (cond->waiting = 0)
{
/*
We ' re the last waiter to is notified or to stop waiting, so
Reset the manual event.
*/
/* Close broadcast gate */
resetevent (Cond->events[broadcast]);
/* Open block gate/
SetEvent (cond->broadcast_block_event);
}
leavecriticalsection (&cond->lock_waiting);
entercriticalsection (mutex);
return result = = Wait_timeout? etimedout:0;
} |
Here is the wait time, where is the event notification? Again we come to the code mentioned in the previous article to create a thread for the new connection:
The following is a code fragment:
void Create_thread_to_handle_connection (THD *thd) { if (Cached_thread_count > Wake_thread) { /* Get thread from Cache */ Thread_cache.append (THD); wake_thread++; Pthread_cond_signal (&cond_thread_cache); } Else ... } |
About MySQL database thread buffer pool related knowledge is introduced here, I hope this introduction can be harvested for you!