好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Linux C線程池簡單實(shí)現(xiàn)實(shí)例

Linux C線程池簡單實(shí)現(xiàn)實(shí)例

熱門標(biāo)簽:網(wǎng)絡(luò)電話外呼系統(tǒng)撥號軟件 天津智能外呼系統(tǒng)排名 貴陽400電話到哪里去辦理 電銷機(jī)器人加盟多少錢 400電話申請找 申請400電話有什么用 汨羅代理外呼系統(tǒng) 宿松高德地圖標(biāo)注 4層電梯外呼控制系統(tǒng)設(shè)計(jì)

Linux C線程池

三個文件 

1 tpool.h

typedef struct tpool_work { 
  void        (*routine)(void *); 
  void        *arg; 
  struct tpool_work  *next; 
} tpool_work_t; 
 
typedef struct tpool { 
  /* pool characteristics */ 
  int         num_threads; 
  int         max_queue_size; 
  /* pool state */ 
  pthread_t      *tpid; 
  tpool_work_t    *queue; 
  int         front, rear; 
  /* 剩下的任務(wù)可以做完, 但不能再加新的任務(wù) */ 
  int         queue_closed;   
  /* 剩下的任務(wù)都不做了, 直接關(guān)閉 */ 
  int         shutdown;     
  /* pool synchronization */ 
  pthread_mutex_t   queue_lock; 
  pthread_cond_t   queue_has_task; 
  pthread_cond_t   queue_has_space; 
  pthread_cond_t   queue_empty; 
} *tpool_t; 
 
void tpool_init(tpool_t *tpoolp,int num_threads, int max_queue_size); 
 
int tpool_add_work(tpool_t tpool,void(*routine)(void *), void *arg); 
 
int tpool_destroy(tpool_t tpool,int finish); 

 2 tpool.c

#include stdlib.h> 
#include stdio.h> 
#include unistd.h> 
#include sys/types.h> 
#include string.h> 
#include pthread.h> 
#include "tpool.h" 
 
#define DEBUG 
 
#if defined(DEBUG) 
#define debug(...) do { \ 
  flockfile(stdout); \ 
  printf("###%p.%s: ", (void *)pthread_self(), __func__); \ 
  printf(__VA_ARGS__); \ 
  putchar('\n'); \ 
  fflush(stdout); \ 
  funlockfile(stdout); \ 
} while (0) 
#else 
#define debug(...) 
#endif 
 
void *tpool_thread(void *); 
 
void tpool_init(tpool_t *tpoolp, int num_worker_threads, int max_queue_size) 
{ 
  int i; 
  tpool_t pool; 
 
  pool = (tpool_t)malloc(sizeof(struct tpool)); 
  if (pool == NULL) { 
    perror("malloc"); 
    exit(0); 
  } 
 
  pool->num_threads = 0; 
  pool->max_queue_size = max_queue_size + 1; 
  pool->num_threads = num_worker_threads; 
  pool->tpid = NULL; 
  pool->front = 0; 
  pool->rear = 0; 
  pool->queue_closed = 0; 
  pool->shutdown = 0; 
 
  if (pthread_mutex_init(pool->queue_lock, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
  if (pthread_cond_init(pool->queue_has_space, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
  if (pthread_cond_init(pool->queue_has_task, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
  if (pthread_cond_init(pool->queue_empty, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
 
  if ((pool->queue = malloc(sizeof(struct tpool_work) *  
          pool->max_queue_size)) == NULL) { 
    perror("malloc"); 
    free(pool); 
    exit(0); 
  } 
 
  if ((pool->tpid = malloc(sizeof(pthread_t) * num_worker_threads)) == NULL) { 
    perror("malloc"); 
    free(pool); 
    free(pool->queue); 
    exit(0); 
  } 
 
  for (i = 0; i  num_worker_threads; i++) { 
    if (pthread_create(pool->tpid[i], NULL, tpool_thread,  
          (void *)pool) != 0) { 
      perror("pthread_create"); 
      exit(0); 
    } 
  } 
 
  *tpoolp = pool; 
} 
 
 
int empty(tpool_t pool) 
{ 
  return pool->front == pool->rear; 
} 
 
int full(tpool_t pool) 
{ 
  return ((pool->rear + 1) % pool->max_queue_size == pool->front); 
} 
 
int size(tpool_t pool) 
{ 
  return (pool->rear + pool->max_queue_size - 
        pool->front) % pool->max_queue_size; 
} 
 
int tpool_add_work(tpool_t tpool, void(*routine)(void *), void *arg) 
{ 
  tpool_work_t *temp; 
 
  pthread_mutex_lock(tpool->queue_lock); 
 
  while (full(tpool)  !tpool->shutdown  !tpool->queue_closed) { 
    pthread_cond_wait(tpool->queue_has_space, tpool->queue_lock); 
  } 
 
  if (tpool->shutdown || tpool->queue_closed) { 
    pthread_mutex_unlock(tpool->queue_lock); 
    return -1; 
  } 
 
  int is_empty = empty(tpool); 
 
  temp = tpool->queue + tpool->rear; 
  temp->routine = routine; 
  temp->arg = arg; 
  tpool->rear = (tpool->rear + 1) % tpool->max_queue_size; 
 
  if (is_empty) { 
    debug("signal has task"); 
    pthread_cond_broadcast(tpool->queue_has_task); 
  } 
 
  pthread_mutex_unlock(tpool->queue_lock);   
 
  return 0; 
} 
 
void *tpool_thread(void *arg) 
{ 
  tpool_t pool = (tpool_t)(arg); 
  tpool_work_t *work; 
 
  for (;;) { 
    pthread_mutex_lock(pool->queue_lock); 
 
    while (empty(pool)  !pool->shutdown) { 
      debug("I'm sleep"); 
      pthread_cond_wait(pool->queue_has_task, pool->queue_lock); 
    } 
    debug("I'm awake"); 
 
    if (pool->shutdown == 1) { 
      debug("exit"); 
      pthread_mutex_unlock(pool->queue_lock); 
      pthread_exit(NULL); 
    } 
 
    int is_full = full(pool); 
    work = pool->queue + pool->front; 
    pool->front = (pool->front + 1) % pool->max_queue_size; 
 
    if (is_full) { 
      pthread_cond_broadcast(pool->queue_has_space); 
    } 
 
    if (empty(pool)) { 
      pthread_cond_signal(pool->queue_empty); 
    } 
 
    pthread_mutex_unlock(pool->queue_lock);   
 
    (*(work->routine))(work->arg); 
  } 
} 
 
int tpool_destroy(tpool_t tpool, int finish) 
{ 
  int   i; 
 
  pthread_mutex_lock(tpool->queue_lock); 
 
  tpool->queue_closed = 1; 
 
  if (finish == 1) { 
    debug("wait all work done"); 
    while (!empty(tpool)) { 
      pthread_cond_wait(tpool->queue_empty, tpool->queue_lock); 
    } 
  } 
  tpool->shutdown = 1; 
 
  pthread_mutex_unlock(tpool->queue_lock); 
 
  pthread_cond_broadcast(tpool->queue_has_task); 
 
  debug("wait worker thread exit"); 
  for (i = 0; i  tpool->num_threads; i++) { 
    pthread_join(tpool->tpid[i], NULL); 
  } 
 
  debug("free thread pool"); 
  free(tpool->tpid); 
  free(tpool->queue); 
  free(tpool); 
} 
 

3 tpooltest.c

#include stdio.h> 
#include pthread.h> 
#include "tpool.h" 
 
char *str[]={"string 0", "string 1", "string 2",  
        "string 3", "string 4", "string 5"}; 
 
void job(void * jobstr) 
{ 
  long i, x; 
 
  for (i = 0; i  100000000; i++) { 
    x = x +i; 
  } 
  printf("%s\n", (char *)jobstr); 
} 
 
int main(void) 
{ 
  int i;  
  tpool_t test_pool; 
 
  tpool_init(test_pool, 8, 20); 
 
  for ( i = 0; i  5; i++) { 
    tpool_add_work(test_pool, job, str[i]); 
  } 
 
  tpool_destroy(test_pool, 1); 
 
  return 0; 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • Linux C中多線程與volatile變量
  • linux下的C\C++多進(jìn)程多線程編程實(shí)例詳解
  • Linux線程退出方式總結(jié)(推薦)
  • linux線程的取消(終止)方法
  • 淺談linux線程切換問題
  • linux線程切換和進(jìn)程切換的方法
  • linux多線程編程(五)
  • linux多線程編程(四)
  • Linux下的多線程編程(三)
  • Linux多線程編程(一)

標(biāo)簽:廣東 昌都 烏蘭察布 撫州 臨沂 連云港 贛州 海北

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux C線程池簡單實(shí)現(xiàn)實(shí)例》,本文關(guān)鍵詞  Linux,線程,池,簡單,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux C線程池簡單實(shí)現(xiàn)實(shí)例》相關(guān)的同類信息!
  • 本頁收集關(guān)于Linux C線程池簡單實(shí)現(xiàn)實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 亚洲春色在线| 青楼大胸奶头晃着喷奶水H漫画| 伊人久久久人综合网站| 欧美vivodeshd免费| 超爆乳中文字幕巨爆乳| 亚洲日韩啪啪无码AAAAA| 午夜精品久久久久久91| 又粗又硬又猛又黄又刺激毛| 动漫毛片网站| 717影院理论午夜伦八戒| 韩国电影日常关系| 超级丰满乳在线乐派影院| 男女XXOO做爰猛烈| 果冻传媒国产传媒| 偷窥中国熟女WC| 双性被室友灌满精H男男视频| 电车里的日日液 (疯狂电车) | 一品村夫| 人与人xxxc96| 当着老公面被cao高H漫画书| 亚洲乱码一区二区三区香蕉| 国产精品卡一卡2卡三卡网站| 一区二区三区精品视频| 范冰冰裸被?高清网站| 97人妻精品一区二区三区免费| 国产又粗又猛又大的视频| 偷偷鲁| 女人被男人狂躁下面在线观看 | 亚马逊跨境电商开店流程及费用 | 动漫女女性h黄漫图| ova《夫人回复术》| 穿进年代文里的狐狸精女配| 德国丝袜丝交video| 好色视频app| 厕所偷窥china中文wc| 性欧美性另类双性人互交| 是不是很久没人弄你了| 巜乳色吐息未删减版在线观看| 女人与拘做受XXXXⅩ视频| 国产的一级毛片完整| 国产二级一片内射视频插放|