国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++線程池的簡單實現方法

2020-01-26 15:20:58
字體:
來源:轉載
供稿:網友

本文以實例形式較為詳細的講述了C++線程池的簡單實現方法。分享給大家供大家參考之用。具體方法如下:

一、幾個基本的線程函數:

1.線程操縱函數:

int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, (void*)(*start_rtn)(void *), void *arg); //創建void pthread_exit(void *retval);            //終止自身int pthread_cancel(pthread_t tid);            //終止其他.發送終止信號后目標線程不一定終止,要調用join函數等待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); //設置分離狀態int pthread_attr_destroy(pthread_attr_t *attr);           //銷毀屬性

 

3.同步函數
互斥鎖

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);                 //通知,喚醒第一個調用pthread_cond_wait()而進入睡眠的線程

5.工具函數

int pthread_equal(pthread_t t1, pthread_t t2); //比較線程IDint pthread_detach(pthread_t tid);       //分離線程pthread_t pthread_self(void);            //自身ID

上述代碼中,線程的cancel和join,以及最后的工具函數,這些函數的參數都為結構體變量,其他的函數參數都是結構體變量指針;品味一下,參數為指針的,因為都需要改變結構體的內容,而參數為普通變量的,則只需要讀內容即可。

二、線程池代碼:

#include <stdio.h>#include <stdlib.h>#include <pthread.h>  //linux環境中多線程的頭文件,非C語言標準庫,編譯時最后要加 -lpthread 調用動態鏈接庫//工作鏈表的結構typedef struct worker {  void *(*process)(void *arg);  //工作函數  void *arg;           //函數的參數  struct worker *next;}CThread_worker;//線程池的結構typedef struct {  pthread_mutex_t queue_lock;   //互斥鎖  pthread_cond_t queue_ready;  //條件變量/信號量  CThread_worker *queue_head;   //指向工作鏈表的頭結點,臨界區  int cur_queue_size;       //記錄鏈表中工作的數量,臨界區  int max_thread_num;       //最大線程數  pthread_t *threadid;      //線程ID  int shutdown;          //開關}CThread_pool;static CThread_pool *pool = NULL;  //一個線程池變量int pool_add_worker(void *(*process)(void *arg), void *arg);  //負責向工作鏈表中添加工作void *thread_routine(void *arg);  //線程例程//線程池初始化void pool_init(int max_thread_num){  int i = 0;  pool = (CThread_pool *) malloc (sizeof(CThread_pool));  //創建線程池  pthread_mutex_init(&(pool->queue_lock), NULL);   //互斥鎖初始化,參數為鎖的地址  pthread_cond_init( &(pool->queue_ready), NULL);   //條件變量初始化,參數為變量地址  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); //創建線程, 參數為線程ID變量地址、屬性、例程、參數  }  pool->shutdown = 0;}//例程,調用具體的工作函數void *thread_routine(void *arg){  printf("starting thread 0x%x/n", (int)pthread_self());  while(1) {    pthread_mutex_lock(&(pool->queue_lock));  //從工作鏈表中取工作,要先加互斥鎖,參數為鎖地址    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));  //等待資源,信號量用于通知。會釋放第二個參數的鎖,以供添加;函數返回時重新加鎖。    }    if(pool->shutdown) {      pthread_mutex_unlock(&(pool->queue_lock));     //結束開關開啟,釋放鎖并退出線程      printf("thread 0x%x will exit/n", (int)pthread_self());      pthread_exit(NULL);   //參數為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)   //檢測結束開關是否開啟,若開啟,則所有線程會自動退出    return -1;  pool->shutdown = 1;  pthread_cond_broadcast( &(pool->queue_ready) );   //廣播,喚醒所有線程,準備退出  int i;  for(i = 0; i < pool->max_thread_num; ++i)    pthread_join(pool->threadid[i], NULL);   //主線程等待所有線程退出,只有join第一個參數不是指針,第二個參數類型是void **,接收exit的返回值,需要強制轉換  free(pool->threadid);  CThread_worker *head = NULL;  while(pool->queue_head != NULL) {      //釋放未執行的工作鏈表剩余結點    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;  //具體的工作函數  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);  //主線程創建線程池,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]);   //主線程負責添加工作,10個工作  }  sleep (5);  pool_destroy();   //銷毀線程池  free (workingnum);  return 0;}

希望本文所述對大家的C++程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永清县| 潮州市| 镇康县| 柞水县| 宜君县| 福安市| 高青县| 岐山县| 安溪县| 贺州市| 昭苏县| 阳曲县| 图们市| 揭阳市| 怀集县| 郸城县| 蛟河市| 万全县| 寿阳县| 剑河县| 焦作市| 桃园县| 平武县| 阳新县| 凤山市| 乐陵市| 海晏县| 望奎县| 襄城县| 治县。| 南丹县| 海丰县| 永福县| 潼南县| 连州市| 墨竹工卡县| 扎囊县| 汾西县| 武川县| 公主岭市| 广饶县|