MySQL database thread buffer pool detailed

Source: Internet
Author: User
Tags current time mutex reset thread

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 (&amp;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-&gt;tv.i64-now.i64)/10000);


/* Don ' t allow the timeout to be negative * *


if (Timeout &lt; 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 &gt; abstime-&gt;max_timeout_msec)


timeout= abstime-&gt;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-&gt;broadcast_block_event, INFINITE);


entercriticalsection (&amp;cond-&gt;lock_waiting);


cond-&gt;waiting++;


leavecriticalsection (&amp;cond-&gt;lock_waiting);


leavecriticalsection (mutex);


result= waitformultipleobjects (2, cond-&gt;events, FALSE, timeout);


entercriticalsection (&amp;cond-&gt;lock_waiting);


cond-&gt;waiting--;


if (cond-&gt;waiting = 0)


{


/*


We ' re the last waiter to is notified or to stop waiting, so


Reset the manual event.


*/


/* Close broadcast gate */


resetevent (Cond-&gt;events[broadcast]);


/* Open block gate/


SetEvent (cond-&gt;broadcast_block_event);


}


leavecriticalsection (&amp;cond-&gt;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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.