.线程池.

线程池总体流程
线程池:一次性创建多个子线程,主线程(生产者)负责接收客户端的链接并创建socket对象然后加入到仓库中,然后子线程(消费者)从仓库中获取socket对象进行服务,当客户端退出时,子线程不销毁而是继续从仓库中获取下一个socket对象进行服务。这种网络编程的优点是限制客户端的链接数量,不需要创建销毁线程,节约了线程销毁和创建的时间,同时空闲的线程会进入休眠,不会与工作线程竞争。所以它的应用范围更广,编程难度更高。

thread.h

#ifndef THREADPOOL_H
#define THREADPOOL_H
#include "queue.h"
#include <pthread.h>

//任务入口函数
typedef void (*EnterFP)(void*);

typedef struct ThreadPool
{
	int thread_cnt;//子线程数量
	pthread_t* tids;//子线程id
	Queue* store;//仓库任务队列
	EnterFP enter;//任务入口函数
	//线程锁
	pthread_mutex_t hlock;//锁头
	pthread_mutex_t tlock;//锁尾
	pthread_cond_t empty;//线程空的条件
	pthread_cond_t full;//线程满的条件
}ThreadPool;

//初始化线程池
ThreadPool* create_threadpool(int thread_cnt,int store_cal,EnterFP enter);
//启动线程池
void start_threadpool(ThreadPool* thread);
//主线程抓取任务到线程池中
void push_threadpool(ThreadPool* thread,void* task);
//线程池弹出任务交给子线程处理
void* pop_threadpool(ThreadPool* thread);
//销毁线程池
void destroy_threadpool(ThreadPool* thread);

#endif//THREADPOOL_H

thread.c

#include <stdlib.h>
#include "threadpool.h"

// 线程入口函数
static void* run(void* arg)
{
	ThreadPool* thread = arg;
	for(;;)
	{
		void* task = pop_threadpool(thread);
		thread->enter(task);
	}
}

//初始化线程下池
ThreadPool* create_threadpool(int thread_cnt,int store_cal,EnterFP enter)
{
	ThreadPool* thread = malloc(sizeof(ThreadPool));
	thread->tids = malloc(sizeof(pthread_t)*thread_cnt);
	thread->store = create_queue(store_cal);
	thread->thread_cnt = thread_cnt;
	thread->enter = enter;

	pthread_mutex_init(&thread->hlock,NULL);
	pthread_mutex_init(&thread->tlock,NULL);
	pthread_cond_init(&thread->empty,NULL);
	pthread_cond_init(&thread->full,NULL);
	
	return thread;
}
//启动线程池
void start_threadpool(ThreadPool* thread)
{
	for(int i=0; i<thread->thread_cnt; i++)
	{
		pthread_create(thread->tids+i,NULL,run,thread);
	}
}
//主线程抓取任务到队列中(入队)
void push_threadpool(ThreadPool* thread,void* task)
{
	pthread_mutex_lock(&thread->tlock);
	while(full_queue(thread->store))
	{
		pthread_cond_signal(&thread->empty);
		pthread_cond_wait(&thread->full,&thread->tlock);
	}
	
	push_queue(thread->store,task);
	pthread_cond_signal(&thread->empty);
	pthread_mutex_unlock(&thread->tlock);
}
//仓库弹出任务交给子线程处理(出队)
void* pop_threadpool(ThreadPool* thread)
{
	pthread_mutex_lock(&thread->hlock);
	while(empty_queue(thread->store))
	{
		pthread_cond_signal(&thread->full);
		pthread_cond_wait(&thread->empty,&thread->hlock);
	}
	
	void* task = front_queue(thread->store);
	pop_queue(thread->store);
	pthread_cond_signal(&thread->full);
	pthread_mutex_unlock(&thread->hlock);
	return task;
}
//销毁线程池
void destroy_threadpool(ThreadPool* thread)
{
	pthread_cond_destroy(&thread->full);	
	pthread_cond_destroy(&thread->empty);
	pthread_mutex_destroy(&thread->tlock);
	pthread_mutex_destroy(&thread->hlock);
	destroy_queue(thread->store);
	free(thread->tids);
	free(thread);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值