一個(gè)簡單的linux線程池
一個(gè)簡單的linux線程池
。線程池:簡單地說,線程池就是預(yù)先創(chuàng)建好一批線程,方便、快速地處理收到的業(yè)務(wù)。比起傳統(tǒng)的到來一個(gè)任務(wù),即時(shí)創(chuàng)建一個(gè)線程來處理,節(jié)省了線程的創(chuàng)建和回收的開銷,響應(yīng)更快,效率更高。在linux中,使用的是posix線程庫,首先介紹幾個(gè)常用的函數(shù)。1線程的創(chuàng)建和取消函數(shù)。pthread_create。創(chuàng)建線程;pthread_join。合并線程;pthread_cancel。取消線程;2線程同步函數(shù);pthread_mutex_lock。pthread_mutex_unlock。pthread_cond_signal。pthread_cond_wait。關(guān)于函數(shù)的詳細(xì)說明,參考man手冊。線程池的實(shí)現(xiàn)。
導(dǎo)讀 。線程池:簡單地說,線程池就是預(yù)先創(chuàng)建好一批線程,方便、快速地處理收到的業(yè)務(wù)。比起傳統(tǒng)的到來一個(gè)任務(wù),即時(shí)創(chuàng)建一個(gè)線程來處理,節(jié)省了線程的創(chuàng)建和回收的開銷,響應(yīng)更快,效率更高。在linux中,使用的是posix線程庫,首先介紹幾個(gè)常用的函數(shù)。1線程的創(chuàng)建和取消函數(shù)。pthread_create。創(chuàng)建線程;pthread_join。合并線程;pthread_cancel。取消線程;2線程同步函數(shù);pthread_mutex_lock。pthread_mutex_unlock。pthread_cond_signal。pthread_cond_wait。關(guān)于函數(shù)的詳細(xì)說明,參考man手冊。線程池的實(shí)現(xiàn)。
![](https://img.51dongshi.com/20250105/wz/18528809352.jpg)
系統(tǒng)大全為您提供?線程池:簡單地說,線程池就是預(yù)先創(chuàng)建好一批線程,方便、快速地處理收到的業(yè)務(wù)。比起傳統(tǒng)的到來一個(gè)任務(wù),即時(shí)創(chuàng)建一個(gè)線程來處理,節(jié)省了線程的創(chuàng)建和回收的開銷,響應(yīng)更快,效率更高。在linux中,使用的是posix線程庫,首先介紹幾個(gè)常用的函數(shù):1線程的創(chuàng)建和取消函數(shù)pthread_create創(chuàng)建線程pthread_join合并線程pthread_cancel取消線程2線程同步函數(shù)pthread_mutex_lockpthread_mutex_unlockpthread_cond_signalpthread_cond_wait關(guān)于函數(shù)的詳細(xì)說明,參考man手冊線程池的實(shí)現(xiàn):線程池的實(shí)現(xiàn)主要分為三部分,線程的創(chuàng)建、添加任務(wù)到線程池中、工作線程從任務(wù)隊(duì)列中取出任務(wù)進(jìn)行處理。主要有兩個(gè)類來實(shí)現(xiàn),CTask,CThreadPool/**執(zhí)行任務(wù)的類,設(shè)置任務(wù)數(shù)據(jù)并執(zhí)行**/C代碼classCTask{protected:stringm_strTaskName;?//任務(wù)的名稱void*m_ptrData;???//要執(zhí)行的任務(wù)的具體數(shù)據(jù)public:CTask(){}CTask(stringtaskName){this->m_strTaskName=taskName;m_ptrData=NULL;}virtualintRun()=0;voidSetData(void*data);??//設(shè)置任務(wù)數(shù)據(jù)};任務(wù)類是個(gè)虛類,所有的任務(wù)要從CTask類中繼承,實(shí)現(xiàn)run接口,run接口中需要實(shí)現(xiàn)的就是具體解析任務(wù)的邏輯。m_ptrData是指向任務(wù)數(shù)據(jù)的指針,可以是簡單數(shù)據(jù)類型,也可以是自定義的復(fù)雜數(shù)據(jù)類型。線程池類/**線程池**/Java代碼classCThreadPool{private:vector
m_vecTaskList;????//任務(wù)列表intm_iThreadNum;??????????????//線程池中啟動的線程數(shù)staticvectorm_vecIdleThread;?//當(dāng)前空閑的線程集合staticvectorm_vecBusyThread;?//當(dāng)前正在執(zhí)行的線程集合staticpthread_mutex_tm_pthreadMutex;??//線程同步鎖staticpthread_cond_tm_pthreadCond;??//線程同步的條件變量protected:staticvoid*ThreadFunc(void*threadData);//新線程的線程函數(shù)staticintMovetoIdle(pthread_ttid);?//線程執(zhí)行結(jié)束后,把自己放入到空閑線程中staticintMovetoBusy(pthread_ttid);?//移入到忙碌線程中去intCreate();?????//創(chuàng)建所有的線程public:CThreadPool(intthreadNum);intAddTask(CTask*task);???//把任務(wù)添加到線程池中intStopAll();};當(dāng)線程池對象創(chuàng)建后,啟動一批線程,并把所有的線程放入空閑列表中,當(dāng)有任務(wù)到達(dá)時(shí),某一個(gè)線程取出任務(wù)并進(jìn)行處理。線程之間的同步用線程鎖和條件變量。這個(gè)類的對外接口有兩個(gè):AddTask函數(shù)把任務(wù)添加到線程池的任務(wù)列表中,并通知線程進(jìn)行處理。當(dāng)任務(wù)到到時(shí),把任務(wù)放入m_vecTaskList任務(wù)列表中,并用pthread_cond_signal喚醒一個(gè)線程進(jìn)行處理。StopAll函數(shù)停止所有的線程Cpp代碼************************************************代碼:××××××××××××××××××××CThread.h#ifndef__CTHREAD#define__CTHREAD#include#include#includeusingnamespacestd;/**執(zhí)行任務(wù)的類,設(shè)置任務(wù)數(shù)據(jù)并執(zhí)行**/classCTask{protected:stringm_strTaskName;?//任務(wù)的名稱void*m_ptrData;???//要執(zhí)行的任務(wù)的具體數(shù)據(jù)public:CTask(){}CTask(stringtaskName){this->m_strTaskName=taskName;m_ptrData=NULL;}virtualintRun()=0;voidSetData(void*data);??//設(shè)置任務(wù)數(shù)據(jù)};/**線程池**/classCThreadPool{private:vectorm_vecTaskList;????//任務(wù)列表intm_iThreadNum;??????????????//線程池中啟動的線程數(shù)staticvectorm_vecIdleThread;?//當(dāng)前空閑的線程集合staticvectorm_vecBusyThread;?//當(dāng)前正在執(zhí)行的線程集合staticpthread_mutex_tm_pthreadMutex;??//線程同步鎖staticpthread_cond_tm_pthreadCond;??//線程同步的條件變量protected:staticvoid*ThreadFunc(void*threadData);//新線程的線程函數(shù)staticintMovetoIdle(pthread_ttid);?//線程執(zhí)行結(jié)束后,把自己放入到空閑線程中staticintMovetoBusy(pthread_ttid);?//移入到忙碌線程中去intCreate();?????//創(chuàng)建所有的線程public:CThreadPool(intthreadNum);intAddTask(CTask*task);???//把任務(wù)添加到線程池中?intStopAll();};#endif?類的實(shí)現(xiàn)為:××××××××××××××××××××CThread.cpp#include"CThread.h"#include#includeusingnamespacestd;voidCTask::SetData(void*data){m_ptrData=data;}vectorCThreadPool::m_vecBusyThread;vectorCThreadPool::m_vecIdleThread;pthread_mutex_tCThreadPool::m_pthreadMutex=PTHREAD_MUTEX_INITIALIZER;pthread_cond_tCThreadPool::m_pthreadCond=PTHREAD_COND_INITIALIZER;CThreadPool::CThreadPool(intthreadNum){this->m_iThreadNum=threadNum;Create();}intCThreadPool::MovetoIdle(pthread_ttid){vector::iteratorbusyIter=m_vecBusyThread.begin();while(busyIter!=m_vecBusyThread.end()){if(tid==*busyIter){break;}busyIter++;}m_vecBusyThread.erase(busyIter);m_vecIdleThread.push_back(tid);return0;}intCThreadPool::MovetoBusy(pthread_ttid){vector::iteratoridleIter=m_vecIdleThread.begin();while(idleIter!=m_vecIdleThread.end()){if(tid==*idleIter){break;}idleIter++;}m_vecIdleThread.erase(idleIter);m_vecBusyThread.push_back(tid);return0;}void*CThreadPool::ThreadFunc(void*threadData){pthread_ttid=pthread_self();while(1){pthread_mutex_lock(&m_pthreadMutex);pthread_cond_wait(&m_pthreadCond,&m_pthreadMutex);cout《"tid:"《tid《"run"《endl;//gettaskvector*taskList=(vector*)threadData;vector::iteratoriter=taskList->begin();while(iter!=taskList->end()){MovetoBusy(tid);break;}CTask*task=*iter;taskList->erase(iter);pthread_mutex_unlock(&m_pthreadMutex);cout《"idelthreadnumber:"《CThreadPool::m_vecIdleThread.size()《endl;cout《"busythreadnumber:"《CThreadPool::m_vecBusyThread.size()《endl;//cout《"tasktoberun:"《taskList->size()《endl;task->Run();//cout《"CThread::threadwork"《endl;cout《"tid:"《tid《"idle"《endl;}return(void*)0;}intCThreadPool::AddTask(CTask*task){this->m_vecTaskList.push_back(task);pthread_cond_signal(&m_pthreadCond);return0;}intCThreadPool::Create(){for(inti=0;i::iteratoriter=m_vecIdleThread.begin();while(iter!=m_vecIdleThread.end()){pthread_cancel(*iter);pthread_join(*iter,NULL);iter++;}iter=m_vecBusyThread.begin();while(iter!=m_vecBusyThread.end()){pthread_cancel(*iter);pthread_join(*iter,NULL);iter++;}return0;}?簡單示例:××××××××test.cpp#include"CThread.h"#includeusingnamespacestd;classCWorkTask:publicCTask{public:CWorkTask(){}intRun(){cout《(char*)this->m_ptrData《endl;sleep(10);return0;}};intmain(){CWorkTasktaskObj;charszTmp[]="thisisthefirstthreadrunning,hahasuccess";taskObj.SetData((void*)szTmp);CThreadPoolthreadPool(10);for(inti=0;i<11;i++){threadPool.AddTask(&taskObj);}while(1){sleep(120);}return0;}??以上就是系統(tǒng)大全給大家介紹的如何使的方法都有一定的了解了吧,好了,如果大家還想了解更多的資訊,那就趕緊點(diǎn)擊系統(tǒng)大全官網(wǎng)吧。??本文來自系統(tǒng)大全http:///如需轉(zhuǎn)載請注明!推薦:win7純凈版
一個(gè)簡單的linux線程池
。線程池:簡單地說,線程池就是預(yù)先創(chuàng)建好一批線程,方便、快速地處理收到的業(yè)務(wù)。比起傳統(tǒng)的到來一個(gè)任務(wù),即時(shí)創(chuàng)建一個(gè)線程來處理,節(jié)省了線程的創(chuàng)建和回收的開銷,響應(yīng)更快,效率更高。在linux中,使用的是posix線程庫,首先介紹幾個(gè)常用的函數(shù)。1線程的創(chuàng)建和取消函數(shù)。pthread_create。創(chuàng)建線程;pthread_join。合并線程;pthread_cancel。取消線程;2線程同步函數(shù);pthread_mutex_lock。pthread_mutex_unlock。pthread_cond_signal。pthread_cond_wait。關(guān)于函數(shù)的詳細(xì)說明,參考man手冊。線程池的實(shí)現(xiàn)。
為你推薦