c++線程池實(shí)現(xiàn)方法
本文實(shí)例講述了c++線程池實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
下面這個(gè)線程池是我在工作中用到過(guò)的,原理還是建立一個(gè)任務(wù)隊(duì)列,讓多個(gè)線程互斥的在隊(duì)列中取出任務(wù),然后執(zhí)行,顯然,隊(duì)列是要加鎖的
環(huán)境:ubuntu linux
文件名:locker.h
#ifndef LOCKER_H_ #define LOCKER_H_ #include "pthread.h" class locker { public: locker(); virtual ~locker(); bool lock(); void unlock(); private: pthread_mutex_t m_mutex; }; #endif /* LOCKER_H_ */
文件名:locker.cpp
#include "locker.h" locker::locker() { pthread_mutex_init(&m_mutex, 0); } locker::~locker() { pthread_mutex_destroy(&m_mutex); } bool locker::lock() { if(0 == pthread_mutex_lock(&m_mutex)) return true; return false; } void locker::unlock() { pthread_mutex_unlock(&m_mutex); }
文件名:task_list.h
#ifndef TASK_LIST_H_ #define TASK_LIST_H_ #include "list" #include "locker.h" #include "netinet/in.h" #include "semaphore.h" using namespace std; typedef void* (*THREAD_FUNC)(void*); // 線程池中運(yùn)行的任務(wù),對(duì)于下行任務(wù),sin中包含目的地址信息 // parm0指向發(fā)出數(shù)據(jù)的對(duì)象,parm1指向數(shù)據(jù),parm2為數(shù)據(jù)的長(zhǎng)度 typedef struct { THREAD_FUNC func; void* parm0; void* parm1; void* parm2; } task_info; typedef list<task_info*> TASK_LIST; typedef list<task_info*>::iterator PTASK_LIST; class task_list { public: task_list(); virtual ~task_list(); void append_task(task_info* tsk); task_info* fetch_task(); private: TASK_LIST m_tasklist; locker m_lk; sem_t m_sem; }; #endif /* TASK_LIST_H_ */
文件名:task_list.cpp
#include "task_list.h" task_list::task_list() { // Init Semaphore sem_init(&m_sem, 0, 0); m_tasklist.clear(); } task_list::~task_list() { while(!m_tasklist.empty()) { task_info* tr = m_tasklist.front(); m_tasklist.pop_front(); if(tr) delete tr; } // Destroy Semaphore sem_destroy(&m_sem); } void task_list::append_task(task_info* tsk) { // Lock before Modify the list m_lk.lock(); m_tasklist.push_back(tsk); m_lk.unlock(); // Increase the Semaphore sem_post(&m_sem); } task_info* task_list::fetch_task() { task_info* tr = NULL; sem_wait(&m_sem); m_lk.lock(); tr = m_tasklist.front(); m_tasklist.pop_front(); m_lk.unlock(); return tr; }
文件名:thread_pool.h
#ifndef THREAD_POOL_H_ #define THREAD_POOL_H_ #include "task_list.h" #include "pthread.h" #define DEFAULT_THREAD_COUNT 4 #define MAXIMUM_THREAD_COUNT 1000 class thread_pool { public: thread_pool(); virtual ~thread_pool(); int create_threads(int n = DEFAULT_THREAD_COUNT); void delete_threads(); void set_tasklist(task_list* plist); void del_tasklist(); protected: static void* thread_func(void* parm); task_info* get_task(); private: int m_thread_cnt; pthread_t m_pids[MAXIMUM_THREAD_COUNT]; task_list* m_tasklist; }; #endif /* THREAD_POOL_H_ */
文件名:thread_pool.cpp
#include "thread_pool.h" thread_pool::thread_pool() { m_thread_cnt = 0; m_tasklist = NULL; } thread_pool::~thread_pool() { delete_threads(); } task_info* thread_pool::get_task() { task_info* tr; if (m_tasklist) { tr = m_tasklist->fetch_task(); return tr; } return NULL; } void* thread_pool::thread_func(void* parm) { thread_pool *ptp = static_cast<thread_pool*> (parm); task_info *task; while (true) { task = ptp->get_task(); if (task) { (*task->func)(task); //delete task; //func負(fù)責(zé)釋放task_info } } return NULL; } int thread_pool::create_threads(int n) { if (n > MAXIMUM_THREAD_COUNT) n = MAXIMUM_THREAD_COUNT; delete_threads(); for (int i = 0; i < n; i++) { int ret = pthread_create(&m_pids[i], NULL, thread_func, (void*) this); if (ret != 0) break; m_thread_cnt++; } return m_thread_cnt; } void thread_pool::delete_threads() { for (int i = 0; i < m_thread_cnt; i++) { void* retval; pthread_cancel(m_pids[i]); pthread_join(m_pids[i], &retval); } m_thread_cnt = 0; } void thread_pool::set_tasklist(task_list* plist) { m_tasklist = plist; } void thread_pool::del_tasklist() { m_tasklist = NULL; }
文件名:test.cpp
#include "unistd.h" #include "stdio.h" #include "stdlib.h" #include "task_list.h" #include "thread_pool.h" void* fun(void *parm) { task_info* ptk = (task_info*)parm; pid_t tid = pthread_self(); int count = (int)ptk->parm0; printf("count=%d, tid=%d\n", count, tid); return NULL; } int main() { int count = 0; thread_pool tp; task_list tl; tp.create_threads(4 - 1); tp.set_tasklist(&tl); while (1) { task_info* pti = NULL; pti = (task_info *) malloc(sizeof(task_info)); pti->func = fun; pti->parm0 = (void *)count; tl.append_task(pti); count++; sleep(2); } // printf("hello,world\n"); return 0; }
編譯運(yùn)行,我是用ecplise建立的automake工程,所以只要修改一下Makefile.am就可以編譯成功了
文件名:Makefile.am
bin_PROGRAMS=test test_SOURCES=test.cpp locker.h locker.cpp \ task_list.h task_list.cpp \ thread_pool.h thread_pool.cpp test_LDADD=-lpthread
執(zhí)行結(jié)果:
count=0, tid=-1219888272 count=1, tid=-1219888272 count=2, tid=-1228280976 count=3, tid=-1236673680 count=4, tid=-1219888272 count=5, tid=-1228280976 count=6, tid=-1236673680 count=7, tid=-1219888272 count=8, tid=-1228280976 count=9, tid=-1236673680
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
上一篇:C++抽獎(jiǎng)程序?qū)崿F(xiàn)方法
欄 目:C語(yǔ)言
下一篇:VisualStudio 使用Visual Leak Detector檢查內(nèi)存泄漏
本文標(biāo)題:c++線程池實(shí)現(xiàn)方法
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3009.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解
- 01-10Linux線程管理必備:解析互斥量與條件變量的詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載