linux/C++实现简单线程池
Reference: https://www.cnblogs.com/alwayswangzi/p/7138154.html
线程池:线程池的存在是为了减小线程的创建和销毁成本,线程池中有固定数量的线程。原理上是一个生产者和一个消费者,生产者将任务添加线程池中;消费者为线程,线程获取线程池中的任务并内进行处理。
面向对象设计:线程池的组成大概分为两个部分:
1. 任务类。
任务类应该为抽象类,提供run接口,将任务的具体操作与抽象类分离开。
2. 线程池管理类。
线程池管理类具体有两个内容:缓存任务队列和具体的线程管理。
缓存任务队列:用来放置添加进线程池的任务,可以是数组也可以是链表。任务队列的生产操作:线程池管理类需要提供一个类成员函数将任务放进缓存队列中,并通过条件变量通知阻塞的线程。另外,需要另一个类成员函数查询缓存队列目前剩余任务数量。任务队列的消费操作:由于对缓存任务队列的取任务操作是多个线程进行的,需要使用互斥锁控制对缓存任务队列的访问。
具体的线程管理:1)线程池管理类构造函数。构造函数的入参为线程池中线程的数量,在构造函数中创建敌营数量的线程。
2)线程的回调函数。
3)退出所有线程的函数。
code:
1. threadPool.h
1 #ifndef _THREAD_POOL_H 2 #define _THREAD_POOL_H 3 #include<vector> 4 #include<string> 5 #include<pthread.h> 6 7 using namespace std; 8 /* 9 Task class. 是抽象类,提供run接口。继承该抽象类生成具体类,在具体类重写run函数。将具体的任务操作和Task类分开。 10 */ 11 class CTask{ 12 public: 13 CTask(){}; 14 CTask(string& taskName): strTaskName(taskName), ptrData(nullptr){}; 15 virtual ~CTask(){}; 16 virtual int run() = 0;//纯虚函数。 17 void setData(void* data); 18 protected: 19 string strTaskName; 20 void* ptrData; 21 }; 22 /* 23 ThreadPool class including cachedList and threadPool. 24 */ 25 class CThreadPool{ 26 public: 27 CThreadPool(int ithreadNum): threadNum(ithreadNum){ 28 printf("I have created %d threads.\n", threadNum); 29 create(); 30 }; 31 ~CThreadPool(); 32 int addTask(CTask* task);//将任务添加进线程 33 int getTaskSize();//获取任务队列中任务的剩余数量 34 int stopAll();//退出所有线程 35 36 private: 37 int threadNum;//线程池的初始化大小 38 static bool shutDown;//线程退出标志 39 static vector<CTask*> cachedList;//任务队列 40 pthread_t* ptrPthread;//指向一个装满线程的数组 41 static pthread_mutex_t pthreadMutex;//互斥锁 42 static pthread_cond_t pthreadCond;//条件变量 43 protected: 44 int create();//产生线程 45 static void* ThreadFunc(void* threadData);//线程的回调函数 46 static int MoveToIdle(pthread_t pthread);//将线程置为空闲状态 47 static int MoveToBusy(pthread_t pthread);//将线程置为忙状态 48 49 }; 50 #endif
2. threadPool.cpp
#include"threadPool.h" #include<cstdio> using namespace std; void CTask::setData(void* data){ ptrData = data; } //静态成员的初始化 vector<CTask*> CThreadPool::cachedList; bool CThreadPool::shutDown = false; pthread_mutex_t CThreadPool::pthreadMutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t CThreadPool::pthreadCond = PTHREAD_COND_INITIALIZER; /* 线程池初始化: 1. 构造函数入参:初始化线程池中线程的数量。 2. 构造函数内部:1)根据线程池大小定义线程数组。 2)创建制定数量的线程,放入数组。 */ int CThreadPool::create(){ ptrPthread = new pthread_t[threadNum]; for(int i = 0; i < threadNum; i++){ pthread_create(&ptrPthread[i], nullptr, ThreadFunc, nullptr); } return 0; }; /* 线程的回调函数: 1. 如果缓存队列中有任务,则加锁->取出任务,执行任务->开锁。 2. 如果缓存队列中没有任务,使用条件变量等待任务的到来。 3. 首先检查线程的状态。如果关闭线程,即shutDown为true,则关闭线程。 */ void* CThreadPool::ThreadFunc(void* data){ pthread_t threadId = pthread_self(); while(1){ pthread_mutex_lock(&pthreadMutex);//加锁,对缓存队列长度的锁。cachedList.size() if(shutDown){ pthread_mutex_unlock(&pthreadMutex); printf("[tid: %lu]\texit\n", pthread_self()); pthread_exit(nullptr); } //如果缓存队列中没有任务,则等待缓存队列中有线程加入。 while(cachedList.size() == 0 && !shutDown){ pthread_cond_wait(&pthreadCond, &pthreadMutex); } //如果缓存队列中有任务,则取任务执行。 printf("[tid: %lu]\trun:", threadId); vector<CTask*>::iterator it = cachedList.begin(); CTask* task; if(it != cachedList.end()){ task = *it; cachedList.erase(it); } pthread_mutex_unlock(&pthreadMutex); task->run(); printf("[tid: %lu\tidle\n], tid"); } return (void*)0; }; /* public方法3:停止所有线程: 1. 如果shutdown的标志已经是true,则退出。 2. 否则:1)唤醒所有等待线程,销毁线程池。 2)清除僵尸线程。 3)销毁信号量和互斥量。 */ int CThreadPool::stopAll(){ if(shutDown) return -1; printf("Now I will wnd all the threads!"); shutDown = true; pthread_cond_broadcast(&pthreadCond); for(int i = 0; i < threadNum; i++){ pthread_join(ptrPthread[i], NULL); } delete[] ptrPthread; ptrPthread = nullptr; pthread_mutex_destroy(&pthreadMutex); pthread_cond_destroy(&pthreadCond); return 0; } /* public方法1:将任务加入线程池。 首先将任务加入任务队列,然后使用信号量通知所有阻塞的线程。 */ int CThreadPool::addTask(CTask* task){ pthread_mutex_lock(&pthreadMutex); cachedList.push_back(task); pthread_mutex_unlock(&pthreadMutex); pthread_cond_signal(&pthreadCond); return 0; } /* public方法2;获得任务队列的大小。 */ int CThreadPool::getTaskSize(){ return cachedList.size(); }
3. main.cpp
#include"threadPool.h" #include<iostream> #include<cstdio> #include<stdlib.h> #include<unistd.h> using namespace std; class MyCTask: public CTask{ public: MyCTask() = default; ~MyCTask(){}; int run(){ printf("%s\n", (char*)ptrData); int x = rand()%4 + 1; sleep(x); return 0; } }; int main(){ MyCTask task; char str[] = "hello, world!"; task.setData((void*) str); //消费者,5个线程 CThreadPool threadPool(5); //生产者,10个任务。 for(int i = 0; i < 10; i++){ threadPool.addTask(&task); } while(1){ printf("There are still %d tasks needed to complete...", threadPool.getTaskSize()); if(threadPool.getTaskSize() == 0){ if(threadPool.stopAll() == -1){ printf("Thread Pool clear.\n"); exit(0); } } sleep(2); printf("2 seconds later...\n"); } return 0; };

浙公网安备 33010602011771号