Linux下创建线程池
来源:互联网 发布:小恶魔提利昂知乎 编辑:程序博客网 时间:2024/06/08 01:21
Linux下创建线程池
1.
#include <stdio.h>#include <malloc.h>#include <pthread.h>#include <assert.h>pthread_t *pthread_id;typedef struct MyStruct{ void *(*function)(void *arg); void * arg; struct work_struct * next;}work_struct;typedef struct pthread{ //线程锁 pthread_mutex_t queue_lock; pthread_cond_t queue_ready; //定义最大线程数 int max_pthread; //等待工作的链表 work_struct *work_list; //等待工作的数目 int work_num; //关闭线程的标志 int flag; //线程ID pthread_t *pthreadid;}pthread_pool;static pthread_pool *pool = NULL;//线程入口函数void* function_entry(void *arg){ printf("the is pthread ox%x\n", pthread_self()); while (1) { //加锁 pthread_mutex_lock(&(pool->queue_lock)); /* *等待队列为0,flag为0----进程阻塞 *flad为非0---------------进程退出 *等待队列不为0,flag为0--创建工作 */ //1.进程阻塞 while (pool->work_num == 0 && pool->flag == 0) { printf("the pthread is ox%x will wait\n", pthread_self()); pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock)); } //2.进程退出 if (pool->flag!=0) { //先解锁 pthread_mutex_unlock(&(pool->queue_lock)); printf("the pthread will exit ox%x \n",pthread_self()); //线程退出 pthread_exit(NULL); } //3.创建工作 printf("the pthread will to work ox%x\n",pthread_self()); //判断是否满足条件 assert(pool->work_num!=0); assert(pool->work_list!=NULL); //取出链表元素 pool->work_num--; work_struct *work = pool->work_list; pool->work_list = work->next; pthread_mutex_unlock(&(pool->queue_lock)); //执行函数 (*(work->function))(work->arg); free(work); work = NULL; }}void creat_pthreadpool(int num){ int i; //分配线程池空间 pool = (pthread_pool *)malloc(sizeof(pthread_pool)); //初始化锁和环境变量 pthread_mutex_init(&(pool->queue_lock),NULL); pthread_cond_init(&(pool->queue_ready),NULL); //初始化结构 pool->flag = 0; pool->max_pthread = num; pool->work_list = NULL; pool->work_num = 0; //分配线程空间 pool->pthreadid = (pthread_t *)malloc(sizeof(pthread_t)*num); //创建线程 for (i = 0;i < num;i++) { pthread_create(&(pool->pthreadid[i]),NULL,function_entry,NULL); }}void join_work(void *(*function)(void *arg),void *arg){ //构造新任务 work_struct *new_work = (work_struct *)malloc(sizeof(work_struct)); new_work->arg = arg; new_work->function = function; new_work->next = NULL; pthread_mutex_lock(&(pool->queue_lock)); //将任务加入到等待队列 work_struct *tmpwork = pool->work_list;//取出头结点 if (tmpwork!=NULL) { while (tmpwork->next!=NULL) { tmpwork = tmpwork->next; } tmpwork->next = new_work; } else { pool->work_list = new_work; } pool->work_num++; assert(pool->work_num!=0); assert(pool->work_list != NULL); pthread_mutex_unlock(&(pool->queue_lock)); //唤醒等待中的线程 pthread_cond_signal(&(pool->queue_ready));}void close_pool(){ int i; //标志置1 pool->flag = 1; //唤醒所有等待中的线程 pthread_cond_broadcast(&(pool->queue_ready)); //等待正在执行的线程 for (i = 0; i < pool->max_pthread; i++) { pthread_join(pool->pthreadid[i],NULL); } free(pool->pthreadid); //销毁队列 work_struct *head = NULL; while (pool->work_list!=NULL) { head = pool->work_list; pool->work_list = head->next; free(head); } //销毁互斥锁和条件变量 pthread_mutex_destroy(&(pool->queue_lock)); pthread_cond_destroy(&(pool->queue_ready)); free(pool);//由malloc创建 //置空指针 pool = NULL;}void print_t(int i){ printf("the pthread is 0x%x,work is %d\n",pthread_self(),i);}void main(){ int i; //1.创建线程池 creat_pthreadpool(5); //2.加入任务 for (i = 0;i < 10;i++) { join_work(print_t,&i); } sleep(5); //3.关闭线程池 close_pool();}
程序运行结果图:
我尝试采用pthread_join代替sleep,结果失败了,pthread_join就一直得不到返回,希望解决此问题的大神留言下,谢谢
参考博客:http://blog.csdn.net/zouxinfox/article/details/3560891
http://blog.csdn.net/ithomer/article/details/6063067
http://blog.csdn.net/ithomer/article/details/5921003
http://blog.csdn.net/ithomer/article/details/5920936
0 0
- linux下线程池创建
- Linux下创建线程池
- linux 下创建线程
- linux下创建线程
- Linux下的通用线程池创建
- Linux下的通用线程池创建
- Linux下的通用线程池创建
- Linux下线程池的创建
- Linux下如何创建线程池
- Linux下线程的创建
- Linux下线程的创建
- Linux下线程的创建
- linux下线程的创建
- linux下线程的创建
- linux下线程的创建
- linux 下线程的创建
- Linux下通用线程池的创建与使用[ZT]
- Linux下通用线程池的创建与使用
- POJ NOI0105-42 画矩形
- 【Get深一度】奈奎斯特最小抽样率和奈奎斯特最大无ISI传输速率
- MySQL中varchar最大长度是多少?
- ios之javascriptCore
- Python入门之requests库的安装与简单使用实例
- Linux下创建线程池
- Maven
- POJ 1079 Ratio 笔记
- JavaScript编程中的对象概念
- 5 个免费的受欢迎的 SQLite 管理工具【申明:来源于网络】
- Codeforces-798D. Mike and distribution
- [java]求数组中第2大的数
- Oracle面试题 表连接 高级排序函数
- 2、TFTP服务(宿主机环境搭建)