线程池
- 概念:线程池是一种线程的使用方式,线程过多会带来调度开销,影响缓存局部性和整体性能。线程池中维护着多个线程,外部只需要提供要处理的数据和处理数据的方法,线程池中的线程就可以进行处理。避免了处理短时间任务时创建和销毁线程的代价。同时可以保证内核的充分使用以及防止过分调度。
- 使用场景:1>需要大量线程来完成任务,并且每个任务执行时间很短。2>对性能高要求的应用。3>服务器有突然并发很多任务的情况存在,因为短时间内产生大量线程可能使内存达到极限,出现错误。
- 使用方法:创建固定数量的线程池,循环从任务队列中获取任务对象,获取到任务对象后,执行任务对象中的任务接口。
#include <cstdio>
#include <iostream>
#include <queue>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
typedef void (*handler_t)(int);
class ThreadTask
{
public:
ThreadTask(){
}
void SetTask(int data, handler_t handler) {
_data = data;
_handler = handler;
}
void Run() {//外部只需要调用Run,不需要关系任务如何处理
return _handler(_data);
}
private:
int _data;//任务中要处理的数据
handler_t _handler;//任务中处理数据的方法
};
#define MAX_THREAD 5
class ThreadPool
{
public:
ThreadPool(int max_thr = MAX_THREAD):_thr_max(max_thr){
pthread_mutex_init(&_mutex, NULL);
pthread_cond_init(&_cond, NULL);
for (int i = 0; i < _thr_max; i++) {
pthread_t tid;
int ret = pthread_create(&tid, NULL, thr_start, this);
if (ret != 0) {
printf("thread create error\n");
exit(-1);
}
}
}
~ThreadPool(){
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_cond);
}
bool TaskPush(ThreadTask &task) {
pthread_mutex_lock(&_mutex);
_queue.push(task);
pthread_mutex_unlock(&_mutex);
pthread_cond_broadcast(&_cond);//唤醒所有线程,谁抢到谁处理
return true;
}
// 类的成员函数,有一个隐藏的默认参数,是this指针
static void *thr_start(void *arg) {
ThreadPool *p = (ThreadPool *) arg;
//不断的从任务队列中取出任务,执行任务的Run接口就可以
//每一个任务节点中包含了要处理的数据,以及如何处理的函数
while(1) {
pthread_mutex_lock(&p->_mutex);
while(p->_queue.empty()) {
pthread_cond_wait(&p->_cond, &p->_mutex);
}
ThreadTask task;
task = p->_queue.front();
p->_queue.pop();
pthread_mutex_unlock(&p->_mutex);
task.Run();//任务的处理要放在解锁之外,因为当前的所保护的时队列的操作
}
return NULL;
}
private:
int _thr_max; // 线程池中线程的最大数量--根据这个初始化创建指定数量的线程
std::queue<ThreadTask> _queue;
pthread_mutex_t _mutex;//保护队列操作的互斥锁
pthread_cond_t _cond;//实现从队列中获取节点的同步条件变量
};
void test_func(int data)
{
int sec = (data % 3) + 1;
printf("tid:%p -- get data:%d , sleep:%d\n", pthread_self(), data, sec);
sleep(sec);
}
void tmp_func(int data) {
printf("tid:%p -- tmp_func\n", pthread_self());
sleep(1);
}
int main()
{
ThreadPool pool;
for(int i = 0; i < 10; i++) {
ThreadTask task;
if (i % 2 == 0) {
task.SetTask(i, test_func);
}else {
task.SetTask(i, tmp_func);
}
pool.TaskPush(task);
}
sleep(1000);
return 0;
}
编译代码:
g++ -std=c++0x threadpool.cpp -o threadpool -pthread -lrt
得到结果: