semi-synchronous/semi-reactor thread pool: The main thread listens to the listen socket and all connected sockets that are received when a client requests a task, inserts the task object into the work task object, and waits for the worker thread on the task queue to obtain the Task object and handle it by competing. The work task queue completes the decoupling between the main thread and the worker thread, but because a task request for the same client connection may be handled by a different thread, this requires that all customer requests be stateless.
specific details, in the code :
#ifndef Threadpool_h#define THREADPOOL_H#include <list>#include <cstdio>#include <exception>#include <pthread.h>#include "locker.h"//thread pool class: template parameter for Task objectTemplate<TypeNameT >classthreadpool{ Public: ThreadPool (intThread_number =8,intMax_requests =10000); ~threadpool ();BOOLAppend (t* request);//The main thread adds a task to the task queuePrivate://Note: (When a thread function is encapsulated in a class, the this pointer is passed into the function as the default parameter, and the thread function parameter (void*) does not match.) //WORKAROUND: Thread functions as static functions because static functions do not have this pointer in C + + Static void* Worker (void* arg);//Engineering thread Run function: Call the Run function, remove the task from the work queue, and call the Task object's handler Process task processing voidRun ();Private:intM_thread_number;//number of threads in the thread pool intm_max_requests;//Maximum number of requestspthread_t* m_threads;//thread array STD:: list< t* >M_workqueue;//Request QueueLocker M_queuelocker;//Mutual exclusion lock for request queueSEM m_queuestat;//Semaphore: Whether a task needs to be handled BOOLM_stop;//Do you need to end the thread};Template<TypeNameT >threadpool< T >::threadpool (intThread_number,intmax_requests): M_thread_number (Thread_number), m_max_requests (max_requests), M_stop (false), M_threads (NULL) {if((Thread_number <=0) || (Max_requests <=0) ) {Throw STD:: Exception (); } m_threads =Newpthread_t[M_thread_number];if(! m_threads) {Throw STD:: Exception (); } for(inti =0; i < Thread_number; ++i) {printf("Create the%dth thread\n", i);if(Pthread_create (m_threads + i, NULL, worker, This) !=0)//Create thread (note that the thread function is a static member function, and the this pointer is used as a parameter to access the member variables of the process pool){Delete[] m_threads;Throw STD:: Exception (); }if(Pthread_detach (M_threads[i]))//Set the worker thread to be out-of-thread (the system will automatically reclaim the thread's resources when the disengagement thread exits) or another thread is required to reclaim the thread's resources{Delete[] m_threads;Throw STD:: Exception (); } }}Template<TypeNameT >threadpool< T >::~threadpool () {Delete[] m_threads; M_stop =true;}//main thread joins new task to task queueTemplate<TypeNameT >BOOLthreadpool< T >::append (t* request) {M_queuelocker.lock ();//Because the task queue is shared by all threads, it needs to be locked first if(M_workqueue.size () > M_max_requests) {M_queuelocker.unlock ();return false; } m_workqueue.push_back (Request); M_queuelocker.unlock (); M_queuestat.post ();//Will signal volume +1, if greater than 0, wake up worker threads waiting for the semaphore return true;}//Thread functionTemplate<TypeNameT >void* threadpool< T >::worker (void* arg) {threadpool* pool = (threadpool*) arg; Pool->run ();returnPool;}//Worker thread Handler: Remove the Task object from the task queue and invoke the Task object's logical handler to complete the client request taskTemplate<TypeNameT >voidthreadpool< T >::run () { while(! M_stop) {m_queuestat.wait ();//wait for signal volume greater than 0M_queuelocker.lock ();//Lock task Queue if(M_workqueue.empty ()) {M_queuelocker.unlock ();Continue; } t* request = M_workqueue.front ();//Remove Task ObjectM_workqueue.pop_front (); M_queuelocker.unlock ();if(! Request) {Continue; } request->process ();//Handle customer Tasks}}#endif
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Semi-synchronous/semi-reactor threading pool implementation