设为首页 加入收藏

TOP

C语言实现一个简单的线程池(二)
2014-11-23 21:34:14 来源: 作者: 【 】 浏览:1
Tags:语言 实现 一个 简单 线程
d_cond_signal(&(pool->queue_cond)); pthread_mutex_unlock(&(pool->queue_mutex)); return 0; } int thread_pool_destroy() { if(pool->is_pool_destroyed)//防止多次销毁 return -1; pool->is_pool_destroyed = 1; pthread_cond_broadcast(&(pool->queue_cond));//通知所有线程线程池销毁了 int i; for(i=0; ithread_num; i++)//等待线程全部执行完 pthread_join(pool->thread_queue[i], NULL); //销毁任务队列 task *temp = NULL; while(pool->task_queue_head) { temp = pool->task_queue_head; pool->task_queue_head = pool->task_queue_head->next; free(temp); } //pool->task_queue_head = NULL; //pool->task_queue_end = NULL; //销毁线程队列 free(pool->thread_queue); pool->thread_queue = NULL; pthread_mutex_destroy(&(pool->queue_mutex)); pthread_cond_destroy(&(pool->queue_cond)); free(pool); pool = NULL; return 0; }
test.c

#include "thread_pool.h"
#include 

void *taskprocess(void *arg)
{
	printf("aaaaaadoing tasksaaaaaaaaa\n");
	usleep(1000);
	return NULL;
}


int main()
{
	thread_pool_init(5);
	int i;
	for(i=1; i<=10; i++)
	{
		thread_pool_add_task(taskprocess,(void *)i);
		usleep(1000);
	}
	sleep(1);
	thread_pool_destroy();
	return 0;
}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C指针原理 (25) 下一篇char* 指向内容不能修改的问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: