-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadpool.h
77 lines (58 loc) · 2.09 KB
/
Threadpool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef THREADPOOL_HPP
#define THREADPOOL_HPP
#include<memory>
#include<iostream>
#include<queue>
#include<thread>
#include<vector>
#include<mutex>
#include<condition_variable>
#include<functional>
#include<future>
#include <utility>
#include"TaskQueue.hpp"
using namespace std;
class ThreadPool
{
public:
//此处可以修改初始的线程数
explicit ThreadPool(const int threads=4):work_threads(vector<thread>(threads)),m_shutdown(false){
}
ThreadPool(const ThreadPool &) = delete;
ThreadPool(ThreadPool &&) = delete;
ThreadPool &operator=(const ThreadPool &) = delete;
ThreadPool &operator=(ThreadPool &&) = delete;
~ThreadPool();
void init();
void shutdown();
template<typename F,typename... Args>//多参数的函数模板
auto submit(F&&f,Args&& ...args)->std::future<decltype(f(args...))>;//多参数的函数模板化
private:
class Work{
public:
Work(ThreadPool*pool,int id):m_pool(pool),m_id(id){//初始化工作线程
std::cout<<"thread begin"<<std::endl;
}
void operator()();//重载()开始工作
private:
int m_id; //工作线程的id
ThreadPool *m_pool;//所属的线程池
};
TaskQue<std::function<void()>>tasks;//std::function 用于包装函数
vector<thread>work_threads;
condition_variable cond;
mutex m_mutex;
bool m_shutdown;
};
template<typename F, typename... Args>
auto ThreadPool::submit(F &&f, Args &&... args) -> future<decltype(f(args...))> {
std::function<decltype(f(args ...))()>func=std::bind(std::forward<F>(f),std::forward<Args>(args) ...);//避免左右值的歧义
auto taskptr=make_shared<packaged_task<decltype(f(args ...))()>>(func);//创建了一个智能指针 std::packaged_task的绑定构造 用于期物的创建
function<void()> task_func=[taskptr](){
(*taskptr)();
};
tasks.push(task_func);//压入任务队列
cond.notify_one();//唤醒其中的一个线程
return taskptr->get_future();//返回一个期物 用于在不同线程调用函数返回值
}
#endif