在线亚洲黄色-在线亚洲观看-在线亚洲电影-在线亚洲成人-岛国大片在线观看免费版-岛国大片在线播放高清

一個(gè)簡單的linux線程池

導(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)。

系統(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: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();};當(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純凈版

為你推薦
資訊專欄
熱門視頻
相關(guān)推薦
linux獲取daemon進(jìn)程的控制臺數(shù)據(jù) LinuxMalloc分析從用戶空間到內(nèi)核空間 修復(fù)Win8電腦的7個(gè)問題辦法 nginx配置示例 Win81怎么設(shè)置默認(rèn)瀏覽器Win81設(shè)置默認(rèn)程序辦法 查看Linux下系統(tǒng)資源占用常用命令 linux中打包和壓縮的理解 linux命令之sort Linux下查找連接了mysql的進(jìn)程 Linux下多線程排序的實(shí)現(xiàn) linux下實(shí)現(xiàn)監(jiān)控進(jìn)程網(wǎng)絡(luò)帶寬 win8系統(tǒng)啟動時(shí)出現(xiàn)應(yīng)用程序沖突怎么辦使用執(zhí) linuxtomcat一鍵維護(hù)腳本系 怎么樣查看Win81WinSxS文件夾實(shí)際大小 如何才能WPS快速輸入商標(biāo)符號WPS快速輸入商標(biāo)符 網(wǎng)易云音樂如何排序網(wǎng)易云音樂歌單排序的方 找回windows8命令提示符的辦法介紹 Win8遠(yuǎn)程桌面登錄如何清除歷史痕跡 Linux011啟動流程分析 Win8系統(tǒng)怎么關(guān)閉smartscreen篩選器 為什么win8設(shè)置了從不休眠還是休眠windows8取消休 Win8系統(tǒng)無法關(guān)閉后臺程序?qū)е码娔X卡的解決方法 有道云筆記如何找回被刪除的文件 win81系統(tǒng)怎樣設(shè)置滑動關(guān)機(jī)方法 Ubuntu時(shí)間管理 Linux文件lscpmvrm Windows81系統(tǒng)IE10瀏覽器如何去除超鏈接下劃線 Linux宕機(jī)后如何重啟最安全 linux下vi編輯器命令大全1 inux虛擬機(jī)網(wǎng)絡(luò)配制方法及遇到問題的解決方法 一樣的Windows8不一樣的概念圖 Win8系統(tǒng)中運(yùn)行命令提示符的辦法 藍(lán)屏錯(cuò)誤代碼是0x0000007F怎么辦 Win8創(chuàng)建圖片密碼與Pin碼 在GNULinux中查看端口占用情況的命令方法 怎么樣linux命令之halt命令 Ubuntu下的用戶和權(quán)限三 win8操作系統(tǒng)如何打開xv格式文件 Win81Update如何禁用OneDrive同步服務(wù) linux中export用法
Top 主站蜘蛛池模板: 欧美日韩免费看 | 91亚洲国产成人久久精品网址 | 欧美精品亚洲精品 | 欧美一级淫片吊带丝袜 | 香蕉视频你懂的 | 国产在线观看免费 | 国产女同一区二区三区五区 | 国产一区二区精品久 | 亚洲精品乱码久久久久久v 亚洲精品免费观看 | 真实的和子乱拍在线观看 | 手机看片91精品一区 | 热久久国产欧美一区二区精品 | 天天曰夜夜曰 | 欧美日韩综合精品一区二区三区 | 综合毛片 | 九九啪| 欧美精品在线播放 | 精品免费久久久久国产一区 | 亚洲欧美韩日 | 国产第一页视频 | 欧美精品国产日韩综合在线 | 欧美精品久久天天躁 | 亚洲视频免费观看 | 亚洲欧洲精品一区二区三区 | 亚洲第七页 | 久久精品综合国产二区 | 欧美aa在线观看 | 欧美日韩亚洲天堂 | 欧美成人一区二区三区在线视频 | 欧美日韩综合网 | 国产成人一区二区三区 | 欧美一区二区三区不卡免费 | 精品国产一区二区三区在线观看 | 色视频在线免费观看 | 午夜精品一区二区三区免费视频 | 九九精品视频一区在线 | 美女视频黄a视频全免费网站下载 | 国产在线啪 | 国产高清在线播放免费观看 | 黄色免费一级视频 | 国产午夜电影在线观看 |