一个简单的线程池,添加任务,做完后销毁线程池
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdbool.h>
#define PTHREAD_MAX 20 //最多有20个线程
#define TASK_FULL_NUM 100 //最多等待100个任务
//任务链表结构体
struct task{
void *(*task_func)(int arg);//任务函数
int task_arg;//任务函数的参数
struct task *next;
};
//线程池
struct pthread_pool{
pthread_t tid[PTHREAD_MAX];//线程号数组
struct task *head;//任务链表的头节点
unsigned int active_pthread_num;//正在运行的线程个数
unsigned int wait_task_num;//正在等待的任务个数
pthread_mutex_t m;//线程池互斥锁
pthread_cond_t v;//线程池条件变量
bool shutdown;//线程销毁标志位 flase:不销毁 true:销毁
};
void *time_sec()//计时线程
{
static int time=0;
while(1)
{
printf(" sec:%d\n",time++);
sleep(1);
}
}
void *f(int arg)//任务函数
{
printf("%#x running\n",(unsigned int)pthread_self());
while(arg)
{
arg--;
sleep(1);
}
printf("%#x end\n",(unsigned int)pthread_self());
}
//死锁清理函数
void cleanup(void *arg)
{
printf("