Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of Thread Manager in Mysql

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about the role of Thread Manager in Mysql. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Thread creation function

As we all know, Mysql is now a plug-in storage engine, as long as the implementation of the specified interface, you can implement your own storage engine. So the thread creation of Mysql is in addition to

Outside the main server framework, the storage engine may also create threads. By setting breakpoints, in the version I debugged, I found two functions that created threads.

Os_thread_create is the threading function of the storage engine innobase, first run aground without research, focus on pthread_create, first look at its source code.

Map- > func=func; map- > param=param

HThread= (HANDLE) _ beginthread ((void (_ cdecl *) (void *)) pthread_start, attr- > dwStackSize? Attr- > dwStackSize: 65535, (void*) map)

The above code first constructs a map structure with the function address and the passed-in parameters as members. The operating system interface, _ beginthread, is then called, but the execution function is not the passed-in function-- func, but pthread_start, with an argument of map. Keep tracking pthread_start.

Func= ((struct pthread_map *) param)-> func

As you can see, the func element of map is called in pthread_start as the body of the function that is actually executed. OK, the function that creates the thread is tracked to this point!

two。 What functions are created when the server starts up?

By setting breakpoints at the two places where the threads were created, it is summarized that when the server starts, the following threads are created.

Threads created by pthread_create:

Threads created by innobase's os_thread_create:

You can also pause to see the threads in the server during debugging, as shown in the following figure:

Thread buffer pool

Mysql supports thread caching. In multithreaded connection mode, if the connection is disconnected, put the thread into the free thread buffer, the next time a connection arrives.

First go to the buffer pool to find out if there are any free threads, then use them, and create them if not. You can set the number of thread buffer pools at startup:

When a connection is disconnected, the cache_thread function is called to add idle threads to the cache for later use. As follows:

Cached_thread_count

< thread_cache_size pthread_cond_signal(&COND_flush_thread_cache); 上面我们的启动参数设置线程缓冲区为10,此时对应代码里面的thread_cache_size = 10,cached_thread_count记录 了此刻cache中的空闲线程数目,只有在cache未满的情况下,才会将新的空闲线程加入缓冲池中。加入到缓冲区其实就是将线 程挂起,pthread_cond_wait函数便是线程等待函数,在此函数中,会调用WaitForMultipleObjects进行事件等待。具体源码 如下: result= WaitForMultipleObjects(2, cond->

Events, FALSE, timeout)

Here is the waiting time, where is the event notification? Once again, we come to the code mentioned in the previous article to create a thread for a new connection:

Pthread_cond_signal & COND_thread_cache)

After reading the above, do you have any further understanding of the role of Thread Manager in Mysql? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report