Linux C下实现线程池

下面是原文链接,我是照着敲的。。。hi.baidu.com/boahegcrmdghots/item/f3ca1a3c2d47fcc52e8ec2e1

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
#include<assert.h>

/*
线程池里所有运行和等待的任务都是一个thread_job
由于所有任务都在链表中,所以是一个链表结构
*/
typedef struct job
{
	/*回调函数,任务运行时会调用次函数,注意也可以声明为其他形式*/
	void * (*process)(void *arg);
	void *arg;
	struct job *next;
}thread_job;

/*线程池结构*/
typedef struct 
{
	pthread_mutex_t queue_lock;
	pthread_cond_t queue_ready;
	/*链表结构,线程池中所有等待任务*/
	thread_job *queue_head;
	/*线程池销毁标记*/
	int destroy;
	/**/
	pthread_t *threadid;
	/*线程池中允许的最大线程数目*/
	int max_thread_num;
	/*等待队列的任务数目*/
	int cur_queue_size;
}thread_pool;


int add_job(void *(*process)(void *arg), void *arg);
void *thread_runtine(void *arg);

/*把线程池指针设为静态*/
static thread_pool *pool = NULL;

/*初始化线程池*/
void pool_init(int max_thread_num)
{
	pool = (thread_pool *)malloc(sizeof(thread_pool));
	pthread_mutex_init(&(pool->queue_lock), NULL);
	pthread_cond_init(&(pool->queue_ready), NULL);
	
	pool->queue_head = NULL;
	
	pool->max_thread_num = max_thread_num;
	pool->cur_queue_size = 0;
	/*为线程池中的各个线程分配空间 做初始化*/
	pool->threadid = (pthread_t *)malloc(max_thread_num*sizeof(pthread_t));
	
	int i = 0;
	/*创建max_thread_num个线程*/
	for(i=0; i<max_thread_num; i++)
	{
	    /*理解线程创建函数各个参数, 每个线程都会执行thread_runtine函数*/
		pthread_create(&(pool->threadid[i]), NULL, thread_runtine, NULL);
	}
}
/*每个线程都会执行runtine函数, 每个thread_rutine函数都是取出一个thread_job处理任务
也就是说,线程池中可能有很多个thread_job需要cur_queue_size个线程去处理,处理前要
先对线程池加锁,保证当前处理任务的线程处理thread_job时不会收到其他线程影响,能够互斥访问*/
void *thread_runtine(void *arg)
{
	printf("starting thread 0x%x\n", (unsigned int)pthread_self());
	while(1)
	{
		/*对线程池加互斥锁*/
		pthread_mutex_lock(&(pool->queue_lock));
		/*如果线程当前的等待队列为0,并且没有被销毁,则处于阻塞状态
		  pthread_cond_wait是一个原子操作,等待前解锁,唤醒后会加锁*/
		while( (pool->cur_queue_size == 0) && (!pool->destroy) )
		{
			printf("thread 0x%x is waiting\n", (unsigned int)pthread_self());
			pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));
		}
		/*线程池销毁了*/
		if(pool->destroy)
		{
			pthread_mutex_lock(&(pool->queue_lock));
			printf("thread 0x%x will exit\n", (unsigned int)pthread_self());
			pthread_exit(NULL);
		}
		printf("thread 0x%x is starting to work\n", (unsigned int)pthread_self());
		assert(pool->cur_queue_size != 0);
		assert(pool->queue_head != NULL);
		
		/*等待队列长度减1,并取出线程池中的线程链表中的头元素*/
		pool->cur_queue_size--;
		thread_job *job = pool->queue_head;
		pool->queue_head = job->next;
		pthread_mutex_unlock(&(pool->queue_lock));
		
		/*调用回调函数,执行任务, */
		(*(job->process))(job->arg);
		free(job);
		job = NULL;
	}
	/*这一句是不可达的*/
	pthread_exit(NULL);
}
/*向线程池中添加任务, 重点理解参数, 每个任务要做的事体现在process回调函数上*/
int pool_add_job(void *(*process)(void *arg), void *arg)
{
	thread_job *newjob = (thread_job *)malloc(sizeof(thread_job));
	newjob->process = process;
	newjob->arg = arg;
	newjob->next = NULL;
	/*对线程池加锁*/
	pthread_mutex_lock(&(pool->queue_lock));
	
	thread_job *member = pool->queue_head;
	if(member != NULL)
	{
		while(member->next != NULL)
			member = member->next;
		member->next = newjob;
	}
	else
	{
		pool->queue_head = newjob;
	}
	assert(pool->queue_head != NULL);
	
	pool->cur_queue_size++;
	
	pthread_mutex_unlock(&(pool->queue_lock));
	/*等待队列中有任务了,唤醒一个等待线程,如果所有线程都在忙碌,这句没有任何作用*/
	pthread_cond_signal(&(pool->queue_ready));
	return 0;
}

int pool_destroy()
{
	if(pool->destroy)
		return -1;
	pool->destroy = 1;
	/*唤醒所有等待线程,线程池要销毁了*/
	pthread_cond_broadcast(&(pool->queue_ready));
	/*阻塞等待线程退出,否则变成僵尸了*/
	int i = 0;
	for(i=0; i<pool->max_thread_num; i++)
	{
		pthread_join(pool->threadid[i], NULL);
	}
	free(pool->threadid);
	
	thread_job *head = NULL;
	while(pool->queue_head != NULL)
	{
		head = pool->queue_head;
		pool->queue_head = pool->queue_head->next;
		free(head);
	}
	/*条件变量和互斥量也要销毁*/
	pthread_mutex_destroy(&(pool->queue_lock));
	pthread_cond_destroy(&(pool->queue_ready));
	
	free(pool);
	pool = NULL;
	return 0;
}

void* myprocess(void *arg)
{
	printf("threadid is 0x%x, working on task %d\n", (unsigned int)pthread_self(), *(int *)arg);
	sleep(1);
	return NULL;
}	

int main(int argc, char **argv)
{
	pool_init(3);/*初始话含有3个线程的线程池*/
	int *workingnum = (int *)malloc(sizeof(int)*10);
	int i = 0;
	for(i=0; i<10; i++)
	{
		workingnum[i] = i;
		pool_add_job(myprocess, &workingnum[i]);
	}
	sleep(5);
	pool_destroy();
	free(workingnum);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在C语言中实现一个简单的线程池可以按照以下步骤进行: 1. 定义线程池结构体:创建一个结构体,包含线程池中的线程数量、任务队列、互斥锁、条件变量等必要的成员。 ```c typedef struct { pthread_t *threads; // 线程数组 int thread_count; // 线程数量 task_queue_t *task_queue; // 任务队列 pthread_mutex_t mutex; // 互斥锁 pthread_cond_t cond; // 条件变量 int shutdown; // 线程池关闭标志 } thread_pool_t; ``` 2. 定义任务队列结构体:创建一个结构体,用于存储任务队列的信息,包括任务数组、任务数量、头尾指针等成员。 ```c typedef struct { task_t **tasks; // 任务数组 int task_count; // 任务数量 int head; // 队列头指针 int tail; // 队列尾指针 } task_queue_t; ``` 3. 定义任务结构体:创建一个结构体,用于存储具体的任务信息,例如任务函数指针和参数。 ```c typedef struct { void (*function)(void *); // 任务函数指针 void *argument; // 任务参数 } task_t; ``` 4. 初始化线程池:创建线程池,并初始化线程数组、任务队列、互斥锁、条件变量等。 5. 添加任务:将任务添加到任务队列中,并通过条件变量通知线程池中的线程有新任务可执行。 6. 线程函数:线程池中的线程函数,循环从任务队列中取出任务并执行。 7. 等待线程池任务完成:可以通过条件变量等待所有任务执行完毕。 8. 销毁线程池:释放线程池中的资源,包括线程数组、任务队列、互斥锁、条件变量等。 这是一个简单的线程池实现的框架,你可以根据自己的需求进行具体的功能扩展和优化。注意在并发环境中使用互斥锁和条件变量来保证线程安全。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值