Detailed description of MySQL database creation thread operations

Source: Internet
Author: User

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.

 
 
  1. int pthread_create(pthread_t *thread_id, pthread_attr_t *attr,  
  2. pthread_handler func, void *param)  
  3. {  
  4. HANDLE hThread;  
  5. struct pthread_map *map;  
  6. DBUG_ENTER("pthread_create");  
  7. if (!(map=malloc(sizeof(*map))))  
  8. DBUG_RETURN(-1);  
  9. map->funcfunc=func; map->paramparam=param;  
  10. pthread_mutex_lock(&THR_LOCK_thread);  
  11. #ifdef __BORLANDC__  
  12. hThread=(HANDLE)_beginthread((void(_USERENTRY *)(void *)) pthread_start,  
  13. attr->dwStackSize ? attr->dwStackSize :  
  14. 65535, (void*) map);  
  15. #else  
  16. hThread=(HANDLE)_beginthread((void( __cdecl *)(void *)) pthread_start, attr->dwStackSize ? attr->dwStackSize : 65535, (void*) map);  
  17. #endif  
  18. DBUG_PRINT("info", ("hThread=%lu",(long) hThread));  
  19. *thread_id=map->pthreadself=hThread;  
  20. pthread_mutex_unlock(&THR_LOCK_thread);  
  21. if (hThread == (HANDLE) -1)  
  22. {  
  23. int error=errno;  
  24. DBUG_PRINT("error",  
  25. ("Can't create thread to handle request (error %d)",error));  
  26. DBUG_RETURN(error ? error : -1);  
  27. }  
  28. VOID(SetThreadPriority(hThread, attr->priority)) ;  
  29. 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.

 
 
  1. pthread_handler_t pthread_start(void *param)  
  2. {  
  3. pthread_handler  
  4. func=((struct pthread_map *) param)->func  
  5. void *func_param=((struct pthread_map *) param)->param;  
  6. my_thread_init();         /* Will always succeed in windows */  
  7. pthread_mutex_lock(&THR_LOCK_thread);   /* Wait for beginthread to return */  
  8. win_pthread_self=((struct pthread_map *) param)->pthreadself;  
  9. pthread_mutex_unlock(&THR_LOCK_thread);  
  10. free((char*) param);            /* Free param from create */  
  11. pthread_exit((void*) (*func)(func_param));  
  12. 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!

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.