A simple Linux thread pool

  

Thread pool: Simply put, a thread pool is a batch of threads created in advance to handle the received business easily and quickly. Compared to the traditional arrival of a task, instant create a thread to deal with, saving the overhead of thread creation and recycling, faster response and higher efficiency.

In linux, the posix thread library is used. First, several commonly used functions are introduced:

1 Thread creation and cancellation functions

pthread_create

Create Threads

pthread_join

Merge Threads

pthread_cancel

Cancel Threads

2 Thread Synchronization Functions

pthread_mutex_lock

pthread_mutex_unlock

pthread_cond_signal

pthread_cond_wait

For a detailed description of the function, refer to the man page

Thread pool implementation:

The implementation of the thread pool is mainly divided into three parts, the creation of threads, the addition of tasks to the thread pool, and the removal of tasks from the task queue by the worker thread.

There are two main classes to implement, CTask, CThreadPool

/**

Classes that perform tasks, set task data and execute

** /

C Code

class CTask

{

protected:

string m_strTaskName; //Name of the task

void* m_ptrData; //specific data of the task to be executed

public:

CTask(){}

CTask(string taskName)

{

this->m_strTaskName = taskName;

m_ptrData = NULL;

}

virtual int Run()= 0;

void SetData(void* data); //Set task data

};

The task class is a virtual class, all tasks must inherit from the CTask class, implement the run interface, What needs to be implemented in the run interface is the logic of the specific parsing task. m_ptrData is a pointer to task data, either a simple data type or a custom complex data type.

Thread pool class

/**

Thread pool

**/

Java code

class CThreadPool

{

private:

vector<CTask*> m_vecTaskList; //task list

int m_iThreadNum; //started in thread pool Threads

static vector<pthread_t> m_vecIdleThread; //current idle thread collection

static vector<pthread_t> m_vecBusyThread; //currently executing thread collection

static Pthread_mutex_t m_pthreadMutex; //thread synchronization lock

static pthread_cond_t m_pthreadCond; //conditional variable for thread synchronization

protected:

static void* ThreadFunc(void * threadData); //New thread thread function

static int MoveToIdle(pthread_t tid); //Put the thread into the idle thread after the thread finishes execution

static int MoveToBusy(pthread_t tid); /Move to busy line Go to

int Create(); //Create all threads

public:

CThreadPool(int threadNum);

int AddTask(CTask * Task); //Add the task to the thread pool

int StopAll();

};

When the thread pool object is created, start a batch of threads, and Put all the threads into the free list. When a task arrives, one thread takes the task and processes it.

Synchronization between threads uses thread locks and condition variables.

There are two external interfaces for this class:

The AddTask function adds the task to the task list of the thread pool and notifies the thread to process it. When the task arrives, put the task into the m_vecTaskList task list and wake up a thread with pthread_cond_signal for processing.

StopAll function stops all threads

Cpp code

************************ ************************

Code:

×××××× ××××××××××××××CThread.h

#ifndef __CTHREAD

#define __CTHREAD< Br>

#include <vector>

#include <string>

#include <pthread.h>

using namespace std;

/**

Class for executing tasks, setting task data and executing

**/

class CTask

{

protected:

string m_strTaskName; //name of the task

void* m_ptrData; //specific data of the task to be executed

public:

CTask(){}

CTask(string taskName)

{

this->m_strTaskName = taskName;

m_ptrData = NULL;

}

virtual int Run()= 0;

void SetData(void* data); //Set task data

};

/**

Thread pool

**/

class CThreadPool

{

private:

vector<CTask*> m_vecTaskList; //task list

int m_iThreadNum; //number of threads started in thread pool

static vector<pthread_t> m_vecIdleThread; //current idle thread Collection

static vector<pthread_t> m_vecBusyThread; //currently executing thread collection

static pthread_mutex_t m_pthreadMutex; //thread synchronization lock

static pthread_cond_t m_pthreadCond; //thread Synchronized conditional variables

protected:

static void* ThreadFunc(void * threadData); //Threading function of new thread

static int MoveToIdle(pthread_t tid); //After the thread finishes executing, put yourself into the idle line中

static int MoveToBusy(pthread_t tid); //Move into a busy thread

int Create(); //Create all threads

public:

CThreadPool(int threadNum);

int AddTask(CTask *task); //Add the task to the thread pool

Copyright © Windows knowledge All Rights Reserved