全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Linux C线程池简单实现实例

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; 
  /* 剩下的任务可以做完, 但不能再加新的任务 */ 
  int         queue_closed;   
  /* 剩下的任务都不做了, 直接关闭 */ 
  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线程池  # 线程池的实例  # Linux C中多线程与volatile变量  # Linux线程退出方式总结(推荐)  # linux线程的取消(终止)方法  # 浅谈linux线程切换问题  # linux线程切换和进程切换的方法  # linux多线程编程(五)  # linux多线程编程(四)  # Linux下的多线程编程(三)  # Linux多线程编程(一)  # 希望能  # 不做  # 谢谢大家  # 做完  # 再加  # tpool_t  # queue_empty  # queue_has_space  # tpool_add_work  # tpoolp  # tpool_init  # queue_has_task  # synchronization  # shutdown  # strong  # pthread_cond_t  # queue_lock  # pthread_mutex_t  # sys  # unistd 


相关文章: 测试制作网站有哪些,测试性取向的权威测试或者网站?  制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?  宝塔建站后网页无法访问如何解决?  Bpmn 2.0的XML文件怎么画流程图  建站之星导航菜单设置与功能模块配置全攻略  建站主机是什么?如何选择适合的建站主机?  C#如何在一个XML文件中查找并替换文本内容  深入理解Android中的xmlns:tools属性  济南网站制作的价格,历城一职专官方网站?  网站微信制作软件,如何制作微信链接?  长沙做网站要多少钱,长沙国安网络怎么样?  C++如何使用std::optional?(处理可选值)  广州美橙建站如何快速搭建多端合一网站?  如何选择高效响应式自助建站源码系统?  如何在IIS管理器中快速创建并配置网站?  如何正确下载安装西数主机建站助手?  网站制作企业,网站的banner和导航栏是指什么?  建站之星如何防范黑客攻击与数据泄露?  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  制作销售网站教学视频,销售网站有哪些?  详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)  如何在腾讯云服务器快速搭建个人网站?  如何做网站制作流程,*游戏网站怎么搭建?  网站建设设计制作营销公司南阳,如何策划设计和建设网站?  公司门户网站制作流程,华为官网怎么做?  制作门户网站的参考文献在哪,小说网站怎么建立?  宁波免费建站如何选择可靠模板与平台?  如何用VPS主机快速搭建个人网站?  如何快速上传自定义模板至建站之星?  音乐网站服务器如何优化API响应速度?  如何在Golang中使用replace替换模块_指定本地或远程路径  网站制作报价单模板图片,小松挖机官方网站报价?  如何在阿里云虚拟服务器快速搭建网站?  义乌企业网站制作公司,请问义乌比较好的批发小商品的网站是什么?  已有域名建站全流程解析:网站搭建步骤与建站工具选择  桂林网站制作公司有哪些,桂林马拉松怎么报名?  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  如何在Tomcat中配置并部署网站项目?  如何选择CMS系统实现快速建站与SEO优化?  mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?  建站之星在线版空间:自助建站+智能模板一键生成方案  如何快速打造个性化非模板自助建站?  佛山企业网站制作公司有哪些,沟通100网上服务官网?  ,怎么用自己头像做动态表情包?  如何通过网站建站时间优化SEO与用户体验?  如何在景安云服务器上绑定域名并配置虚拟主机?  如何在香港免费服务器上快速搭建网站?  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  上海网站制作网站建设公司,建筑电工证网上查询系统入口?  制作表格网站有哪些,线上表格怎么弄? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。