C++線程池的簡單實現(xiàn)方法
本文以實例形式較為詳細(xì)的講述了C++線程池的簡單實現(xiàn)方法。分享給大家供大家參考之用。具體方法如下:
一、幾個基本的線程函數(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ā)送終止信號后目標(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); //通知,喚醒第一個調(diào)用pthread_cond_wait()而進入睡眠的線程
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ù)為指針的,因為都需要改變結(jié)構(gòu)體的內(nèi)容,而參數(shù)為普通變量的,則只需要讀內(nèi)容即可。
二、線程池代碼:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> //linux環(huán)境中多線程的頭文件,非C語言標(biāo)準(zhǔn)庫,編譯時最后要加 -lpthread 調(diào)用動態(tài)鏈接庫
//工作鏈表的結(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; //條件變量/信號量
CThread_worker *queue_head; //指向工作鏈表的頭結(jié)點,臨界區(qū)
int cur_queue_size; //記錄鏈表中工作的數(shù)量,臨界區(qū)
int max_thread_num; //最大線程數(shù)
pthread_t *threadid; //線程ID
int shutdown; //開關(guān)
}CThread_pool;
static CThread_pool *pool = NULL; //一個線程池變量
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)); //等待資源,信號量用于通知。會釋放第二個參數(shù)的鎖,以供添加;函數(shù)返回時重新加鎖。
}
if(pool->shutdown) {
pthread_mutex_unlock(&(pool->queue_lock)); //結(jié)束開關(guā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)); //獲取一個工作后釋放鎖
(*(worker->process))(worker->arg); //做工作
free(worker);
worker = NULL;
}
pthread_exit(NULL);
}
//銷毀線程池
int pool_destroy()
{
if(pool->shutdown) //檢測結(jié)束開關(guān)是否開啟,若開啟,則所有線程會自動退出
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第一個參數(shù)不是指針,第二個參數(shù)類型是void **,接收exit的返回值,需要強制轉(zhuǎn)換
free(pool->threadid);
CThread_worker *head = NULL;
while(pool->queue_head != NULL) { //釋放未執(zhí)行的工作鏈表剩余結(jié)點
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) ); //通知一個等待的線程
return 0;
}
int main(int argc, char **argv)
{
pool_init(3); //主線程創(chuàng)建線程池,3個線程
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個工作
}
sleep (5);
pool_destroy(); //銷毀線程池
free (workingnum);
return 0;
}
希望本文所述對大家的C++程序設(shè)計有所幫助。
相關(guān)文章
C++ 網(wǎng)絡(luò)連通性檢測的實現(xiàn)方法
這篇文章主要介紹了C++ 網(wǎng)絡(luò)連通性檢測的實現(xiàn)方法的相關(guān)資料,這里提供實例幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09

