< thrnum;i++){pthread_create(&thrPool->threads[i],&attr,thrRun,(void*)thrPool);//创建多个线程}//printf("end call %s-----\n",__FUNCTION__);}//摧毁线程池void destroy_threadpool(ThreadPool *pool){pool->shutdown = 1;//开始自爆pthread_cond_broadcast(&pool->not_empty_task);//诱杀 int i = 0;for(i = 0; i < pool->thr_num ; i++){pthread_join(pool->threads[i],NULL);}pthread_cond_destroy(&pool->not_empty_task);pthread_cond_destroy(&pool->empty_task);pthread_mutex_destroy(&pool->pool_lock);free(pool->tasks);free(pool->threads);free(pool);}//添加任务到线程池void addtask(ThreadPool *pool){//printf("begin call %s-----\n",__FUNCTION__);pthread_mutex_lock(&pool->pool_lock);//实际任务总数大于最大任务个数则阻塞等待(等待任务被处理)while(pool->max_job_num <= pool->job_num){pthread_cond_wait(&pool->empty_task,&pool->pool_lock);}int taskpos = (pool->job_push++)%pool->max_job_num;//printf("add task %dtasknum===%d\n",taskpos,beginnum);pool->tasks[taskpos].tasknum = beginnum++;pool->tasks[taskpos].arg = (void*)&pool->tasks[taskpos];pool->tasks[taskpos].task_func = taskRun;pool->job_num++;pthread_mutex_unlock(&pool->pool_lock);pthread_cond_signal(&pool->not_empty_task);//通知包身工//printf("end call %s-----\n",__FUNCTION__);}//任务回调函数void taskRun(void *arg){PoolTask *task = (PoolTask*)arg;int num = task->tasknum;printf("task %d is runing %lu\n",num,pthread_self());sleep(1);printf("task %d is done %lu\n",num,pthread_self());}int main(){create_threadpool(3,20);int i = 0;for(i = 0;i < 50 ; i++){addtask(thrPool);//模拟添加任务}sleep(20);destroy_threadpool(thrPool);return 0;}
三、线程池代码【复杂版】
.h
#ifndef __THREADPOOL_H_#define __THREADPOOL_H_typedef struct threadpool_t threadpool_t;/*** @function threadpool_create* @descCreates a threadpool_t object.* @param thr_numthread num* @param max_thr_nummax thread size* @param queue_max_sizesize of the queue.* @return a newly created thread pool or NULL*/threadpool_t *threadpool_create(int min_thr_num, int max_thr_num, int queue_max_size);/*** @function threadpool_add* @desc add a new task in the queue of a thread pool* @param poolThread pool to which add the task.* @param function Pointer to the function that will perform the task.* @param argument Argument to be passed to the function.* @return 0 if all goes well,else -1*/int threadpool_add(threadpool_t *pool, void*(*function)(void *arg), void *arg);/*** @function threadpool_destroy* @desc Stops and destroys a thread pool.* @param poolThread pool to destroy.* @return 0 if destory success else -1*/int threadpool_destroy(threadpool_t *pool);/*** @desc get the thread num* @pool pool threadpool* @return # of the thread*/int threadpool_all_threadnum(threadpool_t *pool);/*** desc get the busy thread num* @param pool threadpool* return # of the busy thread*/int threadpool_busy_threadnum(threadpool_t *pool);#endif
.c
#include #include 【用于锁住队列高并发服务器04:线程池【1个锁、2个条件变量(一个用于阻塞“取】#include
- 物联网云平台应用于农业气象站
- hgroup元素的使用
- 美团用什么消息队列服务器,美团云发布消息队列服务 应对十倍流量激增的秘密武器
- 计算机模拟细胞 斯坦福,斯坦福大学实现高性能低功耗人工突触,可用于神经网络计算
- ICIP-2018 用于深度图像检索的加权广义平均池化
- ARCH Linux 安装教程 (适用于NVIDIA与INTEL交火平台的Lap
- 消息队列中数一数二的“运动健将”Kafka,解密Kafka高性能背后的秘密
- 服务器文件使用记录表,服务器文件使用记录表
- 总线一:IIC
- 一、安装适用于Windows的Linux子系统