在一个应用程序中,我们需要多次使用线程,也就意味着,我们需要多次创建并销毁线程。而创建并销毁线程的过程势必会消耗内存。而在操作系统中,内存资源是及其宝贵的,所以,我们就提出了线程池的概念。
优点:
- 避免峰值压力下,资源耗尽的风险;
- 节省线程创建 / 销毁带来的时间成本
封装一个线程池:
一堆已经创建好的线程 + 线程安全的任务队列。为了让每一个线程针对不同的任务,有不同的处理方法,让线程灵活起来。向线程池抛入数据的时候,顺便将处理函数一起抛入,线程池中的线程使用函数处理数据即可,最好这个函数由用户自己定义。
这样也降低了外部耦合度:不管任务的处理有任何改变,都跟线程池没有关系,不需要修改线程池的代码。
代码实现:
threadpool.hpp
/*
* 实现一个线程池
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <pthread.h>
#include <stdlib.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 10
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 creat 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指针
//线程入口函数没有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;//实现从队列中获取节点的同步条件变量
};
main.cpp
#include "threadpool.hpp"
#include <unistd.h>
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:%d\n",pthread_self(), data);
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;
}
运行结果: