程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++線程池的簡略完成辦法

C++線程池的簡略完成辦法

編輯:關於C++

C++線程池的簡略完成辦法。本站提示廣大學習愛好者:(C++線程池的簡略完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++線程池的簡略完成辦法正文


本文以實例情勢較為具體的講述了C++線程池的簡略完成辦法。分享給年夜家供年夜家參考之用。詳細辦法以下:

1、幾個根本的線程函數:

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); //比擬線程ID
int pthread_detach(pthread_t tid);       //分別線程
pthread_t pthread_self(void);            //本身ID

上述代碼中,線程的cancel和join,和最初的對象函數,這些函數的參數都為構造體變量,其他的函數參數都是構造體變量指針;咀嚼一下,參數為指針的,由於都須要轉變構造體的內容,而參數為通俗變量的,則只須要讀內容便可。

2、線程池代碼:

#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++法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved