C++線程池的簡(jiǎn)單實(shí)現(xiàn)方法
本文以實(shí)例形式較為詳細(xì)的講述了C++線程池的簡(jiǎn)單實(shí)現(xiàn)方法。分享給大家供大家參考之用。具體方法如下:
一、幾個(gè)基本的線程函數(shù):
1.線程操縱函數(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)線程不一定終止,要調(diào)用join函數(shù)等待 int pthread_join(pthread_t tid, void **retval); //阻塞并等待其他線程
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); //銷毀屬性
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); //銷毀鎖 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); //銷毀 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)入睡眠的線程
5.工具函數(shù)
int pthread_equal(pthread_t t1, pthread_t t2); //比較線程ID int pthread_detach(pthread_t tid); //分離線程 pthread_t pthread_self(void); //自身ID
上述代碼中,線程的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)容即可。
二、線程池代碼:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> //linux環(huá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;
//線程池的結(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; //最大線程數(shù)
pthread_t *threadid; //線程ID
int shutdown; //開(kāi)關(guān)
}CThread_pool;
static CThread_pool *pool = NULL; //一個(gè)線程池變量
int pool_add_worker(void *(*process)(void *arg), void *arg); //負(fù)責(zé)向工作鏈表中添加工作
void *thread_routine(void *arg); //線程例程
//線程池初始化
void pool_init(int max_thread_num)
{
int i = 0;
pool = (CThread_pool *) malloc (sizeof(CThread_pool)); //創(chuàng)建線程池
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)建線程, 參數(shù)為線程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)啟,釋放鎖并退出線程
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);
}
//銷毀線程池
int pool_destroy()
{
if(pool->shutdown) //檢測(cè)結(jié)束開(kāi)關(guān)是否開(kāi)啟,若開(kāi)啟,則所有線程會(huì)自動(dòng)退出
return -1;
pool->shutdown = 1;
pthread_cond_broadcast( &(pool->queue_ready) ); //廣播,喚醒所有線程,準(zhǔn)備退出
int i;
for(i = 0; i < pool->max_thread_num; ++i)
pthread_join(pool->threadid[i], NULL); //主線程等待所有線程退出,只有join第一個(gè)參數(shù)不是指針,第二個(gè)參數(shù)類型是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)); //銷毀鎖和條件變量
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è)等待的線程
return 0;
}
int main(int argc, char **argv)
{
pool_init(3); //主線程創(chuàng)建線程池,3個(gè)線程
int *workingnum = (int *) malloc(sizeof(int) * 10);
int i;
for(i = 0; i < 10; ++i) {
workingnum[i] = i;
pool_add_worker(myprocess, &workingnum[i]); //主線程負(fù)責(zé)添加工作,10個(gè)工作
}
sleep (5);
pool_destroy(); //銷毀線程池
free (workingnum);
return 0;
}
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
- c++版線程池和任務(wù)池示例
- c++實(shí)現(xiàn)簡(jiǎn)單的線程池
- c++實(shí)現(xiàn)簡(jiǎn)單的線程池
- c++線程池實(shí)現(xiàn)方法
- C++11 簡(jiǎn)單實(shí)現(xiàn)線程池的方法
- 基于C++11實(shí)現(xiàn)手寫線程池的示例代碼
- 基于C++17實(shí)現(xiàn)的手寫線程池
- 一種類似JAVA線程池的C++線程池實(shí)現(xiàn)方法
- C++實(shí)現(xiàn)一個(gè)簡(jiǎn)單的線程池的示例代碼
- 使用C++實(shí)現(xiàn)一個(gè)高效的線程池
- C++實(shí)現(xiàn)一個(gè)簡(jiǎn)易線程池的使用小結(jié)
相關(guān)文章
面試常見(jiàn)問(wèn)題之C語(yǔ)言與C++的區(qū)別問(wèn)題
在C中,用static修飾的變量或函數(shù),主要用來(lái)說(shuō)明這個(gè)變量或函數(shù)只能在本文件代碼塊中訪問(wèn),而文件外部的代碼無(wú)權(quán)訪問(wèn),今天重點(diǎn)給大家介紹面試中常見(jiàn)的C語(yǔ)言與C++區(qū)別的問(wèn)題,感興趣的朋友跟隨小編一起看看吧2021-05-05
Qt使用QCustomPlot的實(shí)現(xiàn)示例
QCustomPlot是一個(gè)基于Qt C++的圖形庫(kù),用于繪制和數(shù)據(jù)可視化,并為實(shí)時(shí)可視化應(yīng)用程序提供高性能服務(wù),本文主要介紹了Qt使用QCustomPlot的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01
C++ 網(wǎng)絡(luò)連通性檢測(cè)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++ 網(wǎng)絡(luò)連通性檢測(cè)的實(shí)現(xiàn)方法的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
C語(yǔ)言實(shí)現(xiàn)圖書館管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)圖書館管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C++設(shè)計(jì)模式之適配器模式(Adapter)
這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之適配器模式Adapter,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

