C++線(xiàn)程池的簡(jiǎn)單實(shí)現(xiàn)方法
本文以實(shí)例形式較為詳細(xì)的講述了C++線(xiàn)程池的簡(jiǎn)單實(shí)現(xiàn)方法。分享給大家供大家參考之用。具體方法如下:
一、幾個(gè)基本的線(xiàn)程函數(shù):
1.線(xiàn)程操縱函數(shù):
int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, (void*)(*start_rtn)(void *), void *arg); //創(chuàng)建 void pthread_exit(void *retval); //終止自身 int pthread_cancel(pthread_t tid); //終止其他.發(fā)送終止信號(hào)后目標(biāo)線(xiàn)程不一定終止,要調(diào)用join函數(shù)等待 int pthread_join(pthread_t tid, void **retval); //阻塞并等待其他線(xiàn)程
2.屬性:
int pthread_attr_init(pthread_attr_t *attr); //初始化屬性 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); //設(shè)置分離狀態(tài) int pthread_attr_destroy(pthread_attr_t *attr); //銷(xiāo)毀屬性
3.同步函數(shù)
互斥鎖
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); //初始化鎖 int pthread_mutex_destroy(pthread_mutex_t *mutex); //銷(xiāo)毀鎖 int pthread_mutex_lock(pthread_mutex_t *mutex); //加鎖 int pthread_mutex_trylock(pthread_mutex_t *mutex); //嘗試加鎖,上面lock的非阻塞版本 int pthread_mutex_unlock(pthread_mutex_t *mutex); //解鎖
4.條件變量
int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *cattr); //初始化 int pthread_cond_destroy(pthread_cond_t *cond); //銷(xiāo)毀 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); //等待條件 int pthread_cond_signal(pthread_cond_t *cond); //通知,喚醒第一個(gè)調(diào)用pthread_cond_wait()而進(jìn)入睡眠的線(xiàn)程
5.工具函數(shù)
int pthread_equal(pthread_t t1, pthread_t t2); //比較線(xiàn)程ID int pthread_detach(pthread_t tid); //分離線(xiàn)程 pthread_t pthread_self(void); //自身ID
上述代碼中,線(xiàn)程的cancel和join,以及最后的工具函數(shù),這些函數(shù)的參數(shù)都為結(jié)構(gòu)體變量,其他的函數(shù)參數(shù)都是結(jié)構(gòu)體變量指針;品味一下,參數(shù)為指針的,因?yàn)槎夹枰淖兘Y(jié)構(gòu)體的內(nèi)容,而參數(shù)為普通變量的,則只需要讀內(nèi)容即可。
二、線(xiàn)程池代碼:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> //linux環(huán)境中多線(xiàn)程的頭文件,非C語(yǔ)言標(biāo)準(zhǔn)庫(kù),編譯時(shí)最后要加 -lpthread 調(diào)用動(dòng)態(tài)鏈接庫(kù)
//工作鏈表的結(jié)構(gòu)
typedef struct worker {
void *(*process)(void *arg); //工作函數(shù)
void *arg; //函數(shù)的參數(shù)
struct worker *next;
}CThread_worker;
//線(xiàn)程池的結(jié)構(gòu)
typedef struct {
pthread_mutex_t queue_lock; //互斥鎖
pthread_cond_t queue_ready; //條件變量/信號(hào)量
CThread_worker *queue_head; //指向工作鏈表的頭結(jié)點(diǎn),臨界區(qū)
int cur_queue_size; //記錄鏈表中工作的數(shù)量,臨界區(qū)
int max_thread_num; //最大線(xiàn)程數(shù)
pthread_t *threadid; //線(xiàn)程ID
int shutdown; //開(kāi)關(guān)
}CThread_pool;
static CThread_pool *pool = NULL; //一個(gè)線(xiàn)程池變量
int pool_add_worker(void *(*process)(void *arg), void *arg); //負(fù)責(zé)向工作鏈表中添加工作
void *thread_routine(void *arg); //線(xiàn)程例程
//線(xiàn)程池初始化
void pool_init(int max_thread_num)
{
int i = 0;
pool = (CThread_pool *) malloc (sizeof(CThread_pool)); //創(chuàng)建線(xiàn)程池
pthread_mutex_init(&(pool->queue_lock), NULL); //互斥鎖初始化,參數(shù)為鎖的地址
pthread_cond_init( &(pool->queue_ready), NULL); //條件變量初始化,參數(shù)為變量地址
pool->queue_head = NULL;
pool->cur_queue_size = 0;
pool->max_thread_num = max_thread_num;
pool->threadid = (pthread_t *) malloc(max_thread_num * sizeof(pthread_t));
for (i = 0; i < max_thread_num; i++) {
pthread_create(&(pool->threadid[i]), NULL, thread_routine, NULL); //創(chuàng)建線(xiàn)程, 參數(shù)為線(xiàn)程ID變量地址、屬性、例程、參數(shù)
}
pool->shutdown = 0;
}
//例程,調(diào)用具體的工作函數(shù)
void *thread_routine(void *arg)
{
printf("starting thread 0x%x\n", (int)pthread_self());
while(1) {
pthread_mutex_lock(&(pool->queue_lock)); //從工作鏈表中取工作,要先加互斥鎖,參數(shù)為鎖地址
while(pool->cur_queue_size == 0 && !pool->shutdown) { //鏈表為空
printf("thread 0x%x is waiting\n", (int)pthread_self());
pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock)); //等待資源,信號(hào)量用于通知。會(huì)釋放第二個(gè)參數(shù)的鎖,以供添加;函數(shù)返回時(shí)重新加鎖。
}
if(pool->shutdown) {
pthread_mutex_unlock(&(pool->queue_lock)); //結(jié)束開(kāi)關(guān)開(kāi)啟,釋放鎖并退出線(xiàn)程
printf("thread 0x%x will exit\n", (int)pthread_self());
pthread_exit(NULL); //參數(shù)為void *
}
printf("thread 0x%x is starting to work\n", (int)pthread_self());
--pool->cur_queue_size;
CThread_worker *worker = pool->queue_head;
pool->queue_head = worker->next;
pthread_mutex_unlock (&(pool->queue_lock)); //獲取一個(gè)工作后釋放鎖
(*(worker->process))(worker->arg); //做工作
free(worker);
worker = NULL;
}
pthread_exit(NULL);
}
//銷(xiāo)毀線(xiàn)程池
int pool_destroy()
{
if(pool->shutdown) //檢測(cè)結(jié)束開(kāi)關(guān)是否開(kāi)啟,若開(kāi)啟,則所有線(xiàn)程會(huì)自動(dòng)退出
return -1;
pool->shutdown = 1;
pthread_cond_broadcast( &(pool->queue_ready) ); //廣播,喚醒所有線(xiàn)程,準(zhǔn)備退出
int i;
for(i = 0; i < pool->max_thread_num; ++i)
pthread_join(pool->threadid[i], NULL); //主線(xiàn)程等待所有線(xiàn)程退出,只有join第一個(gè)參數(shù)不是指針,第二個(gè)參數(shù)類(lèi)型是void **,接收exit的返回值,需要強(qiáng)制轉(zhuǎn)換
free(pool->threadid);
CThread_worker *head = NULL;
while(pool->queue_head != NULL) { //釋放未執(zhí)行的工作鏈表剩余結(jié)點(diǎn)
head = pool->queue_head;
pool->queue_head = pool->queue_head->next;
free(head);
}
pthread_mutex_destroy(&(pool->queue_lock)); //銷(xiāo)毀鎖和條件變量
pthread_cond_destroy(&(pool->queue_ready));
free(pool);
pool=NULL;
return 0;
}
void *myprocess(void *arg)
{
printf("threadid is 0x%x, working on task %d\n", (int)pthread_self(), *(int*)arg);
sleep (1);
return NULL;
}
//添加工作
int pool_add_worker(void *(*process)(void *arg), void *arg)
{
CThread_worker *newworker = (CThread_worker *) malloc(sizeof(CThread_worker));
newworker->process = process; //具體的工作函數(shù)
newworker->arg = arg;
newworker->next = NULL;
pthread_mutex_lock( &(pool->queue_lock) ); //加鎖
CThread_worker *member = pool->queue_head; //插入鏈表尾部
if( member != NULL ) {
while( member->next != NULL )
member = member->next;
member->next = newworker;
}
else {
pool->queue_head = newworker;
}
++pool->cur_queue_size;
pthread_mutex_unlock( &(pool->queue_lock) ); //解鎖
pthread_cond_signal( &(pool->queue_ready) ); //通知一個(gè)等待的線(xiàn)程
return 0;
}
int main(int argc, char **argv)
{
pool_init(3); //主線(xiàn)程創(chuàng)建線(xiàn)程池,3個(gè)線(xiàn)程
int *workingnum = (int *) malloc(sizeof(int) * 10);
int i;
for(i = 0; i < 10; ++i) {
workingnum[i] = i;
pool_add_worker(myprocess, &workingnum[i]); //主線(xiàn)程負(fù)責(zé)添加工作,10個(gè)工作
}
sleep (5);
pool_destroy(); //銷(xiāo)毀線(xiàn)程池
free (workingnum);
return 0;
}
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
上一篇:C++直接初始化與復(fù)制初始化的區(qū)別深入解析
欄 目:C語(yǔ)言
本文標(biāo)題:C++線(xiàn)程池的簡(jiǎn)單實(shí)現(xiàn)方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3418.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聚類(lèi)算法
- 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線(xiàn)程管理必備:解析互斥量與條件變量的詳解


閱讀排行
- 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è)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載


