最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年07月05日 09:46:34   投稿:lqh  
這篇文章主要介紹了Linux C線程池簡單實現(xiàn)實例的相關(guān)資料,需要的朋友可以參考下

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; 
} 

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

相關(guān)文章

  • bash scp command not found的解決方法

    bash scp command not found的解決方法

    今天在一系統(tǒng)上運(yùn)行bash scp提示command not found,經(jīng)過如下方法解決了,需要的朋友可以參考下
    2013-03-03
  • Shell實現(xiàn)強(qiáng)制釋放內(nèi)存腳本分享

    Shell實現(xiàn)強(qiáng)制釋放內(nèi)存腳本分享

    這篇文章主要介紹了Shell實現(xiàn)強(qiáng)制釋放內(nèi)存腳本分享,本文直接給出實現(xiàn)代碼,并對每一句代碼都做了講解了,需要的朋友可以參考下
    2015-02-02
  • Linux/Nginx如何查看搜索引擎蜘蛛爬蟲的行為

    Linux/Nginx如何查看搜索引擎蜘蛛爬蟲的行為

    本文給大家介紹Linux/Nginx如何查看搜索引擎蜘蛛爬蟲的行為,清楚蜘蛛的爬行情況對做SEO優(yōu)化有很大的幫助。需要的朋友通過本篇文章學(xué)習(xí)下吧
    2015-10-10
  • 用shell批量修改文件名的方法

    用shell批量修改文件名的方法

    這篇文章主要為大家介紹了shell對文件夾中全部文件的名稱加以批量替換、修改的方法,文中的示例代碼講解詳細(xì),對大家的學(xué)習(xí)或工作有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • Shell $0, $#, $*, $@, $?, $$和命令行參數(shù)的使用

    Shell $0, $#, $*, $@, $?, $$和命令行參數(shù)的使用

    這篇文章主要介紹了Shell $0, $#, $*, $@, $?, $$和命令行參數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 三分鐘學(xué)會Linux基本指令

    三分鐘學(xué)會Linux基本指令

    本文小馬將把Linux一般比較常見的指令給大家一一列舉出來,為了大家忘記某些指令后,可以方便查詢記憶,再次小馬建議,Linux指令并不需要去特別花時間專門記憶,只需要多進(jìn)行操作實現(xiàn)就行,這篇文章主要介紹了Linux基本指令,需要的朋友可以參考下
    2022-12-12
  • 使用bash shell刪除目錄中的特定文件的3種方法

    使用bash shell刪除目錄中的特定文件的3種方法

    這篇文章主要介紹了使用bash shell刪除目錄中的特定文件的3種方法,分別為擴(kuò)展模式匹配符、GLOBIGNORE 變量和find 命令,需要的朋友可以參考下
    2014-06-06
  • shell去掉文件中空行(空白行)的方法詳解

    shell去掉文件中空行(空白行)的方法詳解

    最近要查看的日志文件提取后有很多空行,不利于以前的文件可以進(jìn)行比較了,為了向下兼容,只能取得時候把空行刪除掉
    2013-10-10
  • shell字符串匹配的實現(xiàn)

    shell字符串匹配的實現(xiàn)

    這篇文章主要介紹了shell字符串匹配的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Shell腳本用for循環(huán)遍歷參數(shù)的方法技巧

    Shell腳本用for循環(huán)遍歷參數(shù)的方法技巧

    今天小編就為大家分享一篇關(guān)于Shell腳本用for循環(huán)遍歷參數(shù)的方法技巧,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03

最新評論

鄂州市| 靖远县| 郑州市| 漳平市| 西贡区| 琼海市| 阿克苏市| 于田县| 涟源市| 怀仁县| 苏州市| 聂拉木县| 建宁县| 樟树市| 镇平县| 龙游县| 错那县| 岚皋县| 云安县| 漳州市| 洪江市| 灵山县| 讷河市| 福建省| 宁津县| 南涧| 奉新县| 杭锦后旗| 嘉定区| 湖北省| 浦县| 将乐县| 中超| 福贡县| 改则县| 五大连池市| 鲜城| 灵宝市| 新源县| 治县。| 普兰县|