线程池 C语言

一个简单的线程池,添加任务,做完后销毁线程池

#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("
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值