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