c语言线程池编码示例

thread_pool.c

#include "thread_pool.h"

thread_pool *pool = NULL;
static void * thread_pool_entrance(void *arg)
{
	int thread_id = (int)arg;
	printf("thread %d is created\n",thread_id);

	while(1)
	{
		pthread_mutex_lock(&(pool->queue_mutex));
		while(pool->task_queue_size == 0 && !pool->is_pool_destroyed)//必须用while,防止假唤醒
		{
			pthread_cond_wait(&(pool->queue_cond),&(pool->queue_mutex));//等待的时候会解锁,唤醒后加锁
		}

		if(pool->is_pool_destroyed)
		{
			printf("thread %d exit!!!\n",thread_id);
			pthread_mutex_unlock(&(pool->queue_mutex));//中途退出最容易出错,注意要解锁
			pthread_exit(NULL);
		}

		pool->idle_thread_num--;//线程进入忙碌状态
		//从任务队列中取出任务
		task *work;
		work = pool->task_queue_head;
		pool->task_queue_head = pool->task_queue_head->next;
		if(pool->task_queue_head == NULL)
			pool->task_queue_end = NULL;

		pool->task_queue_size--;

		pthread_mutex_unlock(&(pool->queue_mutex));

		//回调函数
		(*(work->taskfunc))(work->arg);
		pool->idle_thread_num++;//线程空闲
	}
	return NULL;
}


int thread_pool_init(int thread_pool_size)
{
	pool = (thread_pool *)malloc(sizeof(thread_pool));//不要最先给线程池分配空间

	pool->is_pool_destroyed = 0;

	pool->task_queue_head = NULL;
	pool->task_queue_end = NULL;
	pool->task_queue_size = 0;

	pool->thread_num = thread_pool_size;
	pool->thread_queue = (pthread_t *)malloc(thread_pool_size * sizeof(pthread_t));
	pool->idle_thread_num = thread_pool_size;

	//创建线程
	int i, ret;
	for(i=0; i<thread_pool_size; i++)
	{
		ret = pthread_create(&(pool->thread_queue[i]), NULL, thread_pool_entrance, (void *)i);
		if(ret < 0)
		{
			printf("thread create error!!!\n");
			thread_pool_destroy();//注意销毁,避免内存泄漏
			return -1;
		}
	}

	pthread_mutex_init(&(pool->queue_mutex), NULL);
	pthread_cond_init(&(pool->queue_cond), NULL);

	return 0;
}


typedef void *(*taskfunc)(void *arg);
int thread_pool_add_task(taskfunc func, void *arg)
{
	task *newtask;
	newtask = (task *)malloc(sizeof(task));
	newtask->taskfunc = func;
	newtask->arg = arg;
	newtask->next = NULL;

	pthread_mutex_lock(&(pool->queue_mutex));

	if(pool->task_queue_head == NULL)
	{
		pool->task_queue_head = pool->task_queue_end = newtask;
	}
	else
	{
		pool->task_queue_end = pool->task_queue_end->next = newtask;
	}
	pool->task_queue_size++;

	pthread_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; i<pool->thread_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;
}

thread_pool.h

#ifndef THREAD_POOL_H
#define THREAD_POOL_H

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>

typedef struct task
{
	void *(*taskfunc)(void *arg);//声明一个函数指针
	void *arg;//函数的参数
	struct task *next;
}task;

typedef struct thread_pool
{
	task *task_queue_head;//任务队列
	task *task_queue_end;//指向任务队列结尾
	int task_queue_size;

	pthread_t *thread_queue;//线程队列
	int thread_num;
	int idle_thread_num;//空闲线程数

	int is_pool_destroyed;

	pthread_mutex_t queue_mutex;//用来互斥访问任务队列
	pthread_cond_t queue_cond;
}thread_pool;


#ifdef __cplusplus
extern "C"{
#endif

extern thread_pool *pool;
extern int thread_pool_init(int thread_pool_size);
//extern void * thread_pool_entrance(void *arg);
extern int thread_pool_add_task(void *(*taskfunc)(void *arg), void *arg);
extern int thread_pool_destroy();

#ifdef __cplusplus
}
#endif

#endif //THREAD_POOL_H

demo.c

#include "thread_pool.h"
#include <stdio.h>

void *taskprocess(void *arg)
{
	printf("++++++++uuuuuuuuuuuu stan1ey\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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线程池是一种常见的并发编程技术,它可以有效地管理线程,提高程序的并发性能。在 C 语言中,我们可以使用 POSIX 线程库来实现线程池。 以下是一个简单的 C 语言线程池实现: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define MAX_THREADS 10 #define MAX_QUEUE 1000 typedef struct { void (*function)(void *); void *argument; } task_t; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t notify = PTHREAD_COND_INITIALIZER; static task_t task_queue[MAX_QUEUE]; static int queue_head = 0, queue_tail = 0, queue_size = 0; static int shutdown = 0; static pthread_t thread_pool[MAX_THREADS]; static int thread_count = 0; static void *thread_function(void *arg) { while (1) { pthread_mutex_lock(&lock); while (queue_size == 0 && !shutdown) { pthread_cond_wait(&notify, &lock); } if (shutdown) { pthread_mutex_unlock(&lock); pthread_exit(NULL); } void (*task_function)(void *); void *task_argument; task_function = task_queue[queue_head].function; task_argument = task_queue[queue_head].argument; queue_size--; queue_head++; if (queue_head == MAX_QUEUE) { queue_head = 0; } pthread_mutex_unlock(&lock); (*task_function)(task_argument); } return NULL; } int thread_pool_init(int num_threads) { if (num_threads <= 0 || num_threads > MAX_THREADS) { num_threads = MAX_THREADS; } for (int i = 0; i < num_threads; i++) { if (pthread_create(&thread_pool[i], NULL, thread_function, NULL) != 0) { return -1; } thread_count++; } return 0; } int thread_pool_add_task(void (*function)(void *), void *argument) { pthread_mutex_lock(&lock); if (queue_size == MAX_QUEUE) { pthread_mutex_unlock(&lock); return -1; } task_queue[queue_tail].function = function; task_queue[queue_tail].argument = argument; queue_size++; queue_tail++; if (queue_tail == MAX_QUEUE) { queue_tail = 0; } pthread_cond_signal(&notify); pthread_mutex_unlock(&lock); return 0; } int thread_pool_destroy() { pthread_mutex_lock(&lock); shutdown = 1; pthread_cond_broadcast(&notify); pthread_mutex_unlock(&lock); for (int i = 0; i < thread_count; i++) { pthread_join(thread_pool[i], NULL); } return 0; } ``` 使用方法如下: ```c void print_message(void *arg) { char *message = (char *) arg; printf("%s\n", message); free(message); } int main() { thread_pool_init(4); for (int i = 0; i < 8; i++) { char *message = malloc(sizeof(char) * 20); sprintf(message, "Task %d", i); thread_pool_add_task(print_message, message); } thread_pool_destroy(); return 0; } ``` 该示例代码中,我们定义了一个任务结构体 `task_t`,包含了任务函数指针和参数。线程池中维护了一个任务队列,当有任务添加时,将任务加入队列,等待线程池中的线程来执行。线程池的每个线程都会从任务队列中取出一个任务并执行。线程池的销毁通过发送停止信号来实现,即将 `shutdown` 标志设置为 1,并唤醒所有等待条件变量的线程。最后,等待所有线程执行结束,然后退出线程池
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值