-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_pool.h
59 lines (54 loc) · 1.72 KB
/
thread_pool.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
/*
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
*/
#include "mingw.thread.h"
#include "mingw.mutex.h"
#include "mingw.condition_variable.h"
#include "mingw.future.h"
#include <vector>
#include <queue>
#include <functional>
#include <memory>
class ThreadPool
{
public:
explicit ThreadPool(size_t poolSize);
~ThreadPool();
ThreadPool(ThreadPool &&adr) = delete;
ThreadPool(const ThreadPool &adr) = delete;
// pass arguments by value
template <class Func, class... Args>
auto exec(Func func, Args... args) -> std::future<decltype(func(args...))> {
auto promise = std::make_shared< std::promise<decltype(func(args...))> >();
std::future<decltype(func(args...))> future = promise->get_future();
auto task = [this](std::shared_ptr< std::promise<decltype(func(args...))> > p,
Func func, Args... args) {
foo(p, func, args...);
};
{
std::lock_guard<std::mutex> guard(q_mutex);
func_queue.push(std::bind(task, promise, func, args...));
}
isready.notify_one();
return future;
}
private:
std::vector<std::thread> thread_vec;
std::queue< std::function<void()> > func_queue;
bool isalive = true;
std::condition_variable isready;
std::mutex q_mutex;
template <class Func, class... Args>
void foo(std::shared_ptr< std::promise<void> > p,
Func func, Args... args) {
func(args...);
p->set_value();
}
template <class PromPtr, class Func, class... Args>
void foo (PromPtr p, Func func, Args... args) {
p->set_value(func(args...));
};
};