C语言线程池

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

//C语言版本

#define LL_ADD(item,list) do{	\
	item->prev = NULL;			\
	item->next = list;			\
	if (list != NULL) list->prev = item;\
	list = item;				\
}while(0)

#define LL_REMOVE(item, list) do{	\
	if(item->prev != NULL) item->prev->next = item->next; \
	if(item->next != NULL) item->next->prev = item->prev; \
	if(list == item) list = item->next; \
	item->prev = item->next = NULL; \
}while(0)

//执行队列,线程s
struct NWORKER
{
	pthread_t thread;
	struct NMANAGER* pool;
	int terminate;
	struct NWORKER* prev;
	struct NWORKER* next;
};

//任务队列,任务s
struct NJOB
{
	void (*func)(struct NJOB* job);
	void* user_data;
	struct NJOB* prev;
	struct NJOB* next;
};

//管理组件
struct NMANAGER
{
	struct NWORKER* workers;
	struct NJOB* jobs;

	pthread_cond_t jobs_cond;
	pthread_mutex_t jobs_mutex;
};

typedef struct NMANAGER nThreadPool;

static void* nThreadCallback(void* arg)
{
	struct NWORKER* worker = (struct NWORKER*)arg;
	while (1)
	{
		pthread_mutex_lock(&worker->pool->jobs_mutex);
		while (worker->pool->jobs == NULL)
		{
			if (worker->terminate) 
				break;
			pthread_cond_wait(&worker->pool->jobs_cond, &worker->pool->jobs_mutex);
		}
		if (worker->terminate)
		{
			pthread_mutex_unlock(&worker->pool->jobs_mutex);
			break;
		}
		struct NJOB* job = worker->pool->jobs;
		LL_REMOVE(job, worker->pool->jobs);

		pthread_mutex_unlock(&worker->pool->jobs_mutex);

		job->func(job->user_data);
	}
	free(worker);
	pthread_exit(NULL);
}

//Thread Pool Create, API
int nThreadPoolCreate(nThreadPool* pool, int numWorkers)
{
	if (numWorkers < 1)
		numWorkers = 1;
	memset(pool, 0, sizeof(nThreadPool));

	pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
	memcpy(&pool->jobs_cond, &blank_cond, sizeof(pthread_cond_t));

	pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;
	memcpy(&pool->jobs_mutex, &blank_mutex, sizeof(pthread_mutex_t));

	int i = 0;
	for ( i = 0; i < numWorkers; i++)
	{
		struct NWORKER* worker = (struct NWORKER*)malloc(sizeof(struct NWORKER));
		if (worker == NULL)
		{
			perror("malloc");
			return -2;
		}
		memset(worker, 0, sizeof(struct NWORKER));
		worker->pool = pool;

		int ret = pthread_create(worker->thread, NULL, nThreadCallback, worker);
		if (ret)
		{
			perror("thread_create");
			free(worker);
			return -3;
		}
		LL_ADD(worker,pool->workers);
	}

	return 0;
}

int nThreadPoolDestory(nThreadPool* pool)
{
	struct NWORKER* worker = NULL;
	for (worker = pool->workers; worker != NULL; worker = worker->next)
	{
		worker->terminate = 1;
	}
	pthread_mutex_lock(&pool->jobs_mutex);
	pthread_cond_broadcast(&pool->jobs_cond);
	pthread_mutex_unlock(&pool->jobs_mutex);
}

void nThreadPoolPush(nThreadPool* pool, struct NJOB* job)
{
	pthread_mutex_lock(&pool->jobs_mutex);
	LL_ADD(job, pool->jobs);
	pthread_cond_signal(&pool->jobs_cond);

	pthread_mutex_unlock(&pool->jobs_mutex);
}

// 任务队列、工作线程、管理者线程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值