Simple and Easy-to-use thread pool implementation

Source: Internet
Author: User

Simple and Easy-to-use thread pool implementation
0 Preface

Recently, I was writing a module of MySQL Cold Standby server. When I got a little touch on something in the thread pool, I wanted to try writing a simple thread pool trainer.

When this thread pool is created, the thread is generated according to the maximum number of threads.

Then, the job adds the tasks to be run to the thread pool through the add_task interface, and then calls the run function of the thread pool to start running all the tasks. Each thread reads the tasks from the task queue, after processing a task, read the new task until the final task queue is empty.

 

Supplement: Looking back at this design, it is actually not a thread pool, and at most it is a multi-thread execution task.

Add all Task descriptions to the task queue of CThreadPool, and then call the run function of CThreadPool to create a specified number of threads, each thread is mutually exclusive and retrieves tasks from the task queue for execution.

1. Thread Pool Design

The following is a simple description (assuming the task class is named CTasklet ):

1. CThreadPool <CTasklet> thread_pool (MAX_THREAD_NUM );

2. Create a task and add the task to the thread pool.

CTasklet * pTask1 = new CTasklet ();

CTasklet * pTask2 = new CTasklet ();

...

Thread_pool.add_task (pTask1 );

Thread_pool.add_task (pTask2 );

...

3. Call the run method of the thread pool to start executing the task.

Thread_pool.run ();

4. Wait until the task is completed.

Thread_pool.join_thread ();

2 source code

The complete thread pool code is provided below

1/* 2 * file: thread_pool.h 3 * desc: simple thread pool, one-time initialization of task queue and thread pool. 4*5 */6 7 # ifndef _ THREAD_POOL_H _ 8 # define _ THREAD_POOL_H _ 9 10 # include <pthread. h> 11 # include <vector> 12 13 using namespace std; 14 15 template <typename workType> 16 class CThreadPool17 {18 public: 19 typedef void * (thread_func) (void *); 20 21 CThreadPool (int thread_num, size_t stack_size = 10485760); 22 ~ CThreadPool (); 23 24 // Add task 25 int add_task (workType * pTask) to the task queue; 26 27 // create a new thread and execute 28 int run (); 29 30 // wait until all threads finish executing 31 int join_thread (); 32 33 private: 34 int init_thread_attr (); 35 int destroy_thread_attr (); 36 37 int set_thread_stacksize (size_t stack_size); 38 int set_thread_joinable (); 39 40 protected: 41 // thread pool execution function, which must be static42 static void start_routine (void * para ); 43 44 private: 45 pthread_attr_t attr _; 46 static pthread_mutex_t mutex_lock _; 47 static list <workType *> list_task _; 48 49 int thread_num _; // maximum thread count 50 vector <pthread_t> thread_id_vec _; 51}; 52 # endifView Code

 

1 # include "pthread_pool.h" 2 3 template <typename workType> 4 pthread_mutex_t CThreadPool <workType >:: mutex_lock _; 5 6 template <typename workType> 7 list <workType *> CThreadPool <workType *>: list_task _; 8 9 template <typename workType> 10 CThreadPool <workType> :: CThreadPool (int thread_num, size_t stack_size) 11 {12 thread_num _ = thread_num; 13 pthread_mutex_init (& mutex_lock _, NULL); 14 15 init_thread_a Ttr (); 16 set_thread_stacksize (stack_size); 17 set_thread_joinable (); 18} 19 20 template <typename workType> 21 CThreadPool <workType> ::~ CthreadPool () 22 {23 destroy_thread_attr (); 24} 25 26 template <typename workType> 27 int init_thread_attr () 28 {29 return pthread_attr_init (& m_attr ); 30} 31 32 template <typename workType> 33 int CThreadPool <workType >:: destroy_thread_attr () 34 {35 return pthread_attr_destroy (& attr _); 36} 37 38 template <typename workType> 39 int CThreadPool <workType >:: set_thread_stacksize (size_t stack_size) 40 {41 return pthread_attr_setstacksize (& attr _, stack_size); 42} 43 44 template <typename workType> 45 int CThreadPool <workType >:: set_thread_joinable () 46 {47 return pthread_attr_setdetachstate (& attr _, PTHREAD_CREATE_JOINABLE); 48} 49 50 template <typename workType> 51 void CThreadPool <workType >:: start_routine (void * para) 52 {53 workType * pWorkType = NULL; 54 55 while (1) {56 pthread_mutex_l Ock (& mutex_lock _); 57 58 if (list_task _. empty () {59 pthread_mutex_unlock (mutex_lock _); 60 return; 61} 62 63 pWorkType = * (list_task _. begin (); 64 list_task _. pop_front (); 65 pthread_mutex_unlock (& mutex_lock _); 66 67 pWorkType-> run (); 68 delete pWorkType; 69 pWorkType = NULL; 70} 71} 72 73 template <typename workType> 74 int CThreadPool <workType>: add_task (workType * pTask) 75 {76 pthread_mutex _ Lock (& mutex_lock _); 77 list_task _. push_back (pTask); 78 pthread_mutex_unlock (& mutex_lock _); 79 return 0; 80} 81 82 template <typename workType> 83 int CThreadPool <workType >:: run () 84 {85 int rc; 86 pthread_t tid; 87 for (int I = 0; I <thread_num _; ++ I) {88 rc = pthread_create (& tid, & attr _, (thread_func) start_routine, NULL); 89 thread_id_vec _. push_back (tid); 90} 91 return rc; 92} 93 94 templa Te <typename workType> 95 int CThreadPool <workType>: join_thread () 96 {97 int rc = 0; 98 vector <pthread_t>: iterator iter; 99 for (iter = thread_id_vec _. begin (); iter! = Thread_id_vec _. end (); ++ iter) {100 rc = pthread_join (* iter), NULL); 101} 102 thread_id_vec _. clear (); 103 return rc; 104}View Code

 

The test code is as follows:

1 # include <unistd. h> 2 3 # include <iostream> 4 # include <list> 5 6 using namespace std; 7 8 class CTasklet 9 {10 public: 11 CTasklet (int num) {12 num _ = num; 13 cout <"CTasklet ctor create num:" <num _ <endl; 14} 15 16 ~ CTasklet () {17 cout <"CTasklet dtor delete num:" <num _ <endl; 18} 19 20 int run () {21 cout <"CTasklet sleep begin:" <num _ <endl; 22 sleep (num _); 23 cout <"CTasklet sleep end: "<num _ <endl; 24} 25 26 private: 27 int num _; 28}; 29 30 # define MAX_THREAD_NUM 331 int main (int argc, char ** argv) 32 {33 // Step1. create a thread pool 34 CThreadPool <CTasklet> thread_pool (MAX_THREAD_NUM); 35 36 // Step2. create a task and add it to the thread pool 37 for (int I = 0; I <6; ++ I) {38 CTasklet * pTask = new CTasklet (I); 39 thread_pool.add_task (pTask); 40} 41 // Step3. start task 42 thread_pool.run (); 43 // Step4. wait for the task to end 44 thread_pool.join_thread (); 45 46 return 0; 47 48}View Code3 Summary

The above thread pool is the simplest type of thread pool, that is, it is equivalent to enabling n threads to execute tasks when the program is running. There are many considerations for the real thread pool. For example, 1. The number of threads in the thread pool should be dynamically changed; 2. The thread pool can dynamically schedule threads to run tasks to achieve a balanced performance; 3. The thread pool should also be able to record the running time of the task to prevent timeout and so on.

However, at least we have opened our head and implemented a simple thread pool. Next, Let's adjust and improve it step by step on this basis.

PS: For the thread pool, I can think of dynamic increase or decrease of the number of threads, timeout mechanism, and load balancing. I don't know what scenarios need to be considered in the thread pool.


Easy language: I want to write a thread pool and multi-thread operation example for this website.



How to implement a thread pool in java

The simplest method is to use java. util. concurrent. Executors.
Call Executors. newCachedThreadPool () to obtain the buffer Thread Pool
Executors. newFixedThreadPool (int nThreads) to obtain a fixed thread pool

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.