MySQLDatabase creationThreadThe related operations in MySQL are described in this article. To improve system efficiency and reduce the system consumption of frequent thread creation and thread suspension, MySQL uses the concept of thread buffer, that is, if a Connection is disconnected, the thread that carries the thread is not destroyed. Instead, the thread is put into the thread buffer and suspended. When the next new Connection arrives, first, go to the thread buffer to check whether there are Idle threads. If so, use it. If not, create a thread.
1. The thread creates a function.
As you know, Mysql is a plug-in storage engine. You only need to implement the specified interface to implement your own storage engine. Therefore, apart from the primary server framework, the storage engine may also create threads. By setting breakpoints, I found two functions for creating threads in my debugging version.
Pthread_create: Mysql self-use thread creation Function
OS _thread_create: The creation thread function of the storage engine innobase.
OS _thread_create is the thread function of the storage engine innobase. We will not study it first, but focus on pthread_create. First, let's look at its source code.
- int pthread_create(pthread_t *thread_id, pthread_attr_t *attr,
- pthread_handler func, void *param)
- {
- HANDLE hThread;
- struct pthread_map *map;
- DBUG_ENTER("pthread_create");
- if (!(map=malloc(sizeof(*map))))
- DBUG_RETURN(-1);
- map->funcfunc=func; map->paramparam=param;
- pthread_mutex_lock(&THR_LOCK_thread);
- #ifdef __BORLANDC__
- hThread=(HANDLE)_beginthread((void(_USERENTRY *)(void *)) pthread_start,
- attr->dwStackSize ? attr->dwStackSize :
- 65535, (void*) map);
- #else
- hThread=(HANDLE)_beginthread((void( __cdecl *)(void *)) pthread_start, attr->dwStackSize ? attr->dwStackSize : 65535, (void*) map);
- #endif
- DBUG_PRINT("info", ("hThread=%lu",(long) hThread));
- *thread_id=map->pthreadself=hThread;
- pthread_mutex_unlock(&THR_LOCK_thread);
- if (hThread == (HANDLE) -1)
- {
- int error=errno;
- DBUG_PRINT("error",
- ("Can't create thread to handle request (error %d)",error));
- DBUG_RETURN(error ? error : -1);
- }
- VOID(SetThreadPriority(hThread, attr->priority)) ;
- DBUG_RETURN(0);
- }
The code above first constructs a map structure with the function address and input parameters. Then call the interface of the operating system, _ beginthread, but the execution function is not the input function-func, but pthread_start. The parameter is map. Continue to track pthread_start.
- pthread_handler_t pthread_start(void *param)
- {
- pthread_handler
- func=((struct pthread_map *) param)->func
- void *func_param=((struct pthread_map *) param)->param;
- my_thread_init(); /* Will always succeed in windows */
- pthread_mutex_lock(&THR_LOCK_thread); /* Wait for beginthread to return */
- win_pthread_self=((struct pthread_map *) param)->pthreadself;
- pthread_mutex_unlock(&THR_LOCK_thread);
- free((char*) param); /* Free param from create */
- pthread_exit((void*) (*func)(func_param));
- return 0; /* Safety */
- }
It can be seen that the func element of map is called in pthread_start, which serves as the actually executed function body. OK. The function that creates the thread is tracked here!
2. What functions are created when the server is started?
By setting breakpoints in the two creation threads, the following threads are created at server startup.
The thread created by pthread_create:
Create a thread function |
Thread-executed Functions |
Create_shutdown_thread |
Handle_shutdown |
Start_handle_manager |
Handle_manager |
Handle_connections_methods |
Handle_connections_sockets |
The thread created by OS _thread_create of innobase:
Create a thread function |
Thread-executed Functions |
Innobase_start_or_create_for_mysql |
Io_handler_thread4) |
Recv_recovery_from_checkpoint_finish |
Trx_rollback_or_clean_all_without_sess |
Innobase_start_or_create_for_mysql |
Srv_lock_timeout_thread |
|
Srv_error_monitor_thread |
|
Srv_monitor_thread |
|
Srv_master_thread |
You can also pause the thread on the server during the debugging process, for example:
This article describes how to create a thread for a MySQL database!