linux线程池示例

56 篇文章 2 订阅

吐舌头池类封装:

/*************************************************************************
	> File Name: tpool.h
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Thu 16 Mar 2017 06:17:34 PM PDT
 ************************************************************************/

#ifndef _TPOOL_H
#define _TPOOL_H

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

typedef struct task {
	void* (*func)(void*);
	void* arg;
	struct task* next;
}task_t;

typedef struct tpool {
	pthread_mutex_t mutex;
	pthread_cond_t cond;	
	task_t *mytask;			// 任务队列的队头
	pthread_t *tid;			// 线程id的指针
}tpool_t;

tpool_t* create_tpool(int num);
int tpool_add_task(tpool_t *tp, void* (*func)(void*), void *arg);

#endif // _TPOOL_H

吐舌头方法实现

/*************************************************************************
	> File Name: tpool.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Thu 16 Mar 2017 06:29:57 PM PDT
 ************************************************************************/

#include "tpool.h"

static void *do_task(void* arg)
{
	tpool_t *tp = (tpool_t*)arg;

	while(1) {
		// 操作共享数据,加锁避免竞态
		pthread_mutex_lock(&tp->mutex);
			// 查看条件(任务链中是否有待处理的任务)
			while(tp->mytask == NULL) {
				// 任务链为空,解锁,加入等待队列,休眠,等待唤醒
				pthread_cond_wait(&tp->cond, &tp->mutex);
			}
			// 摘取任务节点
			task_t *tmp = tp->mytask;
			tp->mytask = tmp->next;
		// 任务摘取完,解锁
		pthread_mutex_unlock(&tp->mutex);
		// 执行取到节点的任务
		tmp->func(tmp->arg);
		free(tmp);
	}
}
// 创建一个线程池对象
tpool_t* create_tpool(int num)
{
	tpool_t *tp = (tpool_t*)malloc(sizeof(tpool_t));
	// 线程池对象初始化
	pthread_mutex_init(&tp->mutex, NULL);
	pthread_cond_init(&tp->cond, NULL);
	tp->mytask = NULL;
	tp->tid = (pthread_t*)malloc(sizeof(pthread_t)*num); 
	// 在该线程池中创建num个线程,传入任务处理接口
	while(num--) {
		pthread_create(&tp->tid[num], NULL, do_task, tp);
	}

	return tp;
}

// 往某个线程池对象添加任务(线程池对象初始化)
// @tp 往哪个线程池添加任务
// @func 传入具体任务
int tpool_add_task(tpool_t* tp, void *(*func)(void*), void *arg)
{
	task_t* newtask = (task_t*)malloc(sizeof(task_t));
	
	newtask->func = func;
	newtask->arg = arg;
	newtask->next = NULL;
	// 含任务func的节点插入任务链中,避免竞态
	pthread_mutex_lock(&tp->mutex);
		newtask->next = tp->mytask;
		tp->mytask = newtask;
	pthread_mutex_unlock(&tp->mutex);
	// 任务添加完毕,广播条件(有新任务加入任务链)
	pthread_cond_broadcast(&tp->cond);

	return 1;
}

吐舌头测试函数

/*************************************************************************
	> File Name: test.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Thu 16 Mar 2017 06:51:38 PM PDT
 ************************************************************************/

#include "tpool.h"

void *thread_func(void* arg)
{
	printf("[%lu]: %d\n", pthread_self(), (int)arg);
	sleep(1);
}

int main()
{
	tpool_t* tp = create_tpool(10);
	
	int n = 1000;

	while(n--) {
		tpool_add_task(tp, thread_func, (void*)n);
	}

	pause();
	return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值