线程池的简介及线程池封装

线程池:线程池就是首先创建一些线程,它们的集合称为线程池。使用线程池可以很好地提高性能,线程池在系统启动时即创建大量空闲的线程,程序将一个任务传给线程池,线程池就会启动一条线程来执行这个任务,执行结束以后,该线程并不会死亡,而是再次返回线程池中成为空闲状态,等待执行下一个任务。
线程池的伸缩性对性能有较大的影响。
创建太多线程,将会浪费一定的资源,有些线程未被充分使用。
销毁太多线程,将导致之后浪费时间再次创建它们。
创建线程太慢,将会导致长时间的等待,性能变差。
销毁线程太慢,导致其它线程资源饥饿。

threadpool.h

#ifndef THREADPOOL_H
#define THREADPOOL_H

#include "array_queue.h"
typedef struct ThreadPool
{
	pthread_t* tid;
	size_t thread_cnt;
	void (*task)(void*);
	ArrayQueue* queue;
	pthread_mutex_t mutex;
	pthread_cond_t full;
	pthread_cond_t null;
}ThreadPool;

// 创建线程池
ThreadPool* tp_create(size_t cnt,size_t cal,void (*task)(void*));

// 销毁线程池
void tp_destory(ThreadPool* tp);

// 启动
void start(ThreadPool* tp);

// 生产
void tp_push(ThreadPool* tp,void* ptr);

// 消费
void* tp_pop(ThreadPool* tp);

#endif//THREADPOOL_H

threadpool.c

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

// 创建线程池
ThreadPool* tp_create(size_t cnt,size_t cal,void (*task)(void*))
{
	ThreadPool* tp = malloc(sizeof(ThreadPool));
	tp->thread_cnt = cnt;
	tp->queue = create_queue(cal);
	tp->tid = malloc(sizeof(pthread_t)*cnt);
	tp->task = task;
	pthread_mutex_init(&tp->mutex,NULL);
	pthread_cond_init(&tp->full,NULL);
	pthread_cond_init(&tp->null,NULL);
	return tp;
}

// 销毁线程池
void tp_destory(ThreadPool* tp)
{
	for(int i=0; i<tp->thread_cnt; i++)
	{
		pthread_cancel(tp->tid[i]);
	}
	free(tp->tid);
	destory_queue(tp->queue);
	free(tp);
}

void* run(void* arg)
{
	ThreadPool* tp = arg;

	for(;;)
	{
		void* ptr = tp_pop(tp);
		tp->task(ptr);
	}
}

// 启动
void start(ThreadPool* tp)
{
	for(int i=0; i<tp->thread_cnt; i++)
	{
		pthread_create(&tp->tid[i],NULL,run,tp);
		printf("启动线程:%lu\n",tp->tid[i]);
	}
}

// 生产
void tp_push(ThreadPool* tp,void* ptr)
{
	pthread_mutex_lock(&tp->mutex);
	while(full_queue(tp->queue))
	{
		pthread_cond_wait(&tp->full,&tp->mutex);
	}
	printf("tp_push:%p\n",ptr);

	push_queue(tp->queue,ptr);
	pthread_mutex_unlock(&tp->mutex);
	pthread_cond_signal(&tp->null);
}

// 消费
void* tp_pop(ThreadPool* tp)
{
	pthread_mutex_lock(&tp->mutex);
	while(empty_queue(tp->queue))
	{
		pthread_cond_wait(&tp->null,&tp->mutex);
	}

	void* ptr = pop_queue(tp->queue);
	printf("tp_pop:%p\n",ptr);

	pthread_mutex_unlock(&tp->mutex);
	pthread_cond_signal(&tp->full);
	return ptr;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值