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
ubuntu gedit Chinese garbled looks very annoying, this article provides two solutions, terminal comm
First, the interrupt system hardware architecture: arm cortex-A9, the interrupt controller on A15 i
A. Static library concept 1. A library is a collection of pre-compiled object files that can be lin
All files in the Linux system are shielded by the virtual file system (VFS) mechanism. Users
Linux configuration ip address
How to make Linux run on U disk
The simplest way to build Ubuntu+Nginx+PHP
Ubuntu update kernel switch kernel boot
How the linux route command works
Linux system virtual memory space
How to view logs in Linux system (common commands)
Never execute the 10 most dangerous commands in Linux
Set the permissions of multiple groups on a file under linux (setfaclgetfacl) method - management
How to open the Win7 notebook touchpad?
Adjusting Windows 7 partitions requires only three steps
Win2003 through the system file checker to help you solve the DLL file loss problem
English version of Win 8 Chinese display garbled solution
Windows8 and common software installation and settings
Win7 flagship version of the winmgmt.exe file error and the solution
How to adjust the zoom of ATI graphics card under Windows 8 system?
Win XP virtual mode failure reason
How does the win7 system import Google Chrome bookmarks into IE7/8/9/10/11 favorites?