c++11简单线程池实现(帮助理解线程池原理)
本文的源代码来自于B站up主的视频,我自己全部用了C++11的特性改写了一遍,视频链接放在最后,感觉讲的挺好的。本文的代码稍微做了一些修改,由于视频中未使用到c++11的一库,像线程库thread,互斥锁mutex,条件变量condition_variable,本案例包含3个文件,分别为threadpool.h、threadpool.cpp、main.cpp,下面给出三个文件的代码。
刚学线程池时因为有些老师说线程池是线程执行玩一个任务放回池子里,当时就不理解,一个线程执行完了不销毁的情况下还能去执行别的任务?让我懵逼了很久,现在来看其实逻辑是在一个工作线程中设置一个死循环不断的取出任务函数然后执行函数。
(1)关闭一个工作线程(worker)的逻辑就是就是让管理者线程(manager)通知worker在死循环中return出去来关闭线程。
(2)开启一个新的线程就是threadpool.cpp中的第162行pool->threadIDs[i].get_id() == thread::id();一个线程不管是否退出都可以使用get_id()方法,如果线程退出了,返回的就是一个默认的id(),这个id()是thread类中的一个嵌套类。
//threadpool.h
#ifndef __THREADPOOL_H__
#define __THREADPOOL_H__
#include <thread>
#include <mutex>
#include <queue>
#include <vector>
#include <condition_variable>
using namespace std;
using callback = void(*)(void*);
class Task
{
public:
callback function;
void* arg;
public:
Task(callback f, void* arg) { function = f; this->arg = arg; }
};
class ThreadPool
{
public:
ThreadPool(int min, int max);
void Add(void(*func)(void*),void* arg);
void Add(Task task);
int Busynum();
int Alivenum();
~ThreadPool();
private:
queue<Task> taskQ;
thread managerID; //管理者线程ID
vector<thread> threadIDs; //
int minNum; //最小线程数
int maxNum; //最大线程数
int busyNum; //忙的线程数
int liveNum; //存活的线程数
int exitNum; //要销毁的线程数
mutex mutexPool; //整个线程池的锁
condition_variable cond; //任务队列是否为空
bool shutdown; //是否销毁线程池,销毁为1,不销毁为0
static void manager(void* arg); //管理者线程
static void worker(void* arg); //工作线程
};
#endif
//threadpool.cpp
#include "threadpool.h"
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
const int NUMBER = 2;
ThreadPool::ThreadPool(int min, int max)
{
do
{
minNum = min;
maxNum = max;
busyNum = 0;
liveNum = min;
exitNum = 0;
shutdown = false;
managerID = thread(manager,this);
threadIDs.resize(max);
for(int i = 0; i < min; ++i)
{
threadIDs[i] = thread(worker,this);
}
return;
} while (0);
}
ThreadPool::~ThreadPool()
{
shutdown = true;
//阻塞回收管理者线程
if(managerID.joinable()) managerID.join();
//唤醒阻塞的消费者线程
cond.notify_all();
for(int i = 0; i < maxNum; ++i)
{
if(threadIDs[i].joinable()) threadIDs[i].join();
}
}
void ThreadPool::Add(Task t)
{
unique_lock<mutex> lk(mutexPool);
if(shutdown)
{
return;
}
//添加任务
taskQ.push(t);
cond.notify_all();
}
void ThreadPool::Add(callback f, void* a)
{
unique_lock<mutex> lk(mutexPool);
if(shutdown)
{
return;
}
//添加任务
taskQ.push(Task(f,a));
cond.notify_all();
}
int ThreadPool::Busynum()
{
mutexPool.lock();
int busy = busyNum;
mutexPool.unlock();
return busy;
}
int ThreadPool::Alivenum()
{
mutexPool.lock();
int alive = liveNum;
mutexPool.unlock();
return alive;
}
void ThreadPool::worker(void* arg)
{
ThreadPool* pool = static_cast<ThreadPool*>(arg);
while(true)
{
unique_lock<mutex> lk(pool->mutexPool);
//当前任务队列是否为空
while(pool->taskQ.empty() && !pool->shutdown)
{
//阻塞工作线程
pool->cond.wait(lk);
if(pool->exitNum > 0)
{
pool->exitNum--;
if(pool->liveNum > pool->minNum)
{
pool->liveNum--;
cout << "threadid: " << pthread_self() << " exit......" << endl;
lk.unlock();
return;
}
}
}
//判断线程池是否关闭了
if(pool->shutdown)
{
cout << "threadid: " << pthread_self() << "exit......" << endl;
return;
}
//从任务队列中去除一个任务
Task task = pool->taskQ.front();
pool->taskQ.pop();
pool->busyNum++;
//解锁
lk.unlock();
cout << "thread: " << pthread_self() << " start working..." << endl;
task.function(task.arg);
//(*task.function)(task.arg);
free(task.arg);
task.arg = nullptr;
cout << "thread: " << pthread_self() << " end working..." << endl;
lk.lock();
pool->busyNum--;
lk.unlock();
}
}
void ThreadPool::manager(void* arg)
{
ThreadPool* pool = static_cast<ThreadPool*>(arg);
while(!pool->shutdown)
{
//每隔3秒检测一次
sleep(3);
//取出线程池中任务的数量和当前线程的数量
unique_lock<mutex> lk(pool->mutexPool);
int queuesize = pool->taskQ.size();
int livenum = pool->liveNum;
int busynum = pool->busyNum;
lk.unlock();
//添加线程
//任务的个数>存活的线程个数 && 存活的线程数 < 最大线程数
if(queuesize > livenum && livenum <pool->maxNum)
{
lk.lock();
int count = 0;
for(int i = 0; i < pool->maxNum && count < NUMBER && pool->liveNum < pool->maxNum; ++i)
{
if(pool->threadIDs[i].get_id() == thread::id())
{
cout << "Create a new thread..." << endl;
pool->threadIDs[i] = thread(worker,pool);
count++;
pool->liveNum++;
}
}
lk.unlock();
}
//销毁线程
//忙的线程*2 < 存活的线程数 && 存活的线程数 > 最小的线程数
if(busynum*2 < livenum && livenum > pool->minNum)
{
lk.lock();
pool->exitNum = NUMBER;
lk.unlock();
//让工作的线程自杀
for(int i = 0; i < NUMBER; ++i) pool->cond.notify_all();
}
}
}
//main.cpp
#include "threadpool.h"
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
void taskFunc(void* arg)
{
int num = *(int*)arg;
cout << "thread: " << pthread_self() << ", number=" << num << endl;
sleep(1);
}
int main()
{
ThreadPool pool(5,10);
int i;
for(i = 0; i < 100; ++i)
{
int* num = new int(i+100);
pool.Add(taskFunc, (void*)num);
}
for(;i < 200; ++i)
{
sleep(1);
int* num = new int(i+100);
pool.Add(taskFunc, (void*)num);
}
return 0;
}
主要和视频中的区别是:
一、没有实现线程安全队列,主要影响在于threadpool.cpp的第119到121之间的线程锁粒度的问题,不过本文这里也是没有运行逻辑问题的,因为使用了线程池的锁,粒度大了一点而已。
二、因为使用了c++11的thread库,不需要threadpooldestroy函数了,直接return退出线程函数,一个线程就会销毁了。
视频链接:
c++版
C语言版