-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.h
More file actions
115 lines (88 loc) · 2.71 KB
/
thread_pool.h
File metadata and controls
115 lines (88 loc) · 2.71 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef __THREAD_POOL__CPP__
#define __THREAD_POOL__CPP___
#include "thread_utility.h"
#include "task_tree.h"
#include "exception.h"
#include <memory>
#include <vector>
#include <thread>
#include <future>
#include <atomic>
#include <type_traits>
// REMOVE
#include <iostream>
using namespace std;
namespace NThread {
class TFunctionWrapper {
private:
struct TImplBase {
virtual void Call() = 0;
virtual ~TImplBase() {}
};
template <typename TFunc>
struct TImpl: public TImplBase {
TImpl(TFunc&& function) : Function_(std::move(function)) {}
void Call() {
Function_();
}
TFunc Function_;
};
public:
template<typename TFunc>
TFunctionWrapper(TFunc&& function) : Impl_(new TImpl<TFunc>(std::move(function))) {}
TFunctionWrapper() = default;
TFunctionWrapper(TFunctionWrapper&& other) : Impl_(std::move(other.Impl_)) {}
TFunctionWrapper& operator=(TFunctionWrapper&& other);
void operator()() {
Impl_->Call();
}
TFunctionWrapper(const TFunctionWrapper&) = delete;
TFunctionWrapper& operator=(const TFunctionWrapper&) = delete;
private:
std::unique_ptr<TImplBase> Impl_;
};
class TThreadPool {
private:
void WorkerThread();
public:
TThreadPool(unsigned int requestedThreadCount);
~TThreadPool();
template <typename TFunc>
std::future<typename std::result_of<TFunc()>::type> SubmitTask(TFunc function);
template <typename TData>
std::shared_future<TData> SubmitTree(NTask::TTaskTree<TData>& tree);
private:
std::vector<std::thread> Threads_;
TThreadSafeQueue<TFunctionWrapper> TaskQueue_;
std::atomic_bool Done_;
TThreadJoiner Joiner_;
};
template <typename TFunc>
std::future<typename std::result_of<TFunc()>::type> TThreadPool::SubmitTask(TFunc function) {
using TResult = typename std::result_of<TFunc()>::type;
std::packaged_task<TResult()> task(std::move(function));
std::future<TResult> result(task.get_future());
TaskQueue_.Push(std::move(task));
return result;
}
template <typename TData>
std::shared_future<TData> TThreadPool::SubmitTree(NTask::TTaskTree<TData>& tree) {
Ensure(tree.Tasks_.size() > 0);
Ensure(tree.Tasks_[0].size() > 0);
std::shared_future<TData> resultFuture = tree.Tasks_[0][0]->SharedFuture();
for (size_t layer = tree.Tasks_.size() - 1; layer != (size_t)-1; layer--) {
for (size_t index = 0; index < tree.Tasks_[layer].size(); index++) {
auto& taskPtr = tree.Tasks_[layer][index];
NTask::TTask<TData>* castedTaskPtr = dynamic_cast<NTask::TTask<TData>*>(taskPtr.get());
if (castedTaskPtr) {
this->SubmitTask(std::move(*castedTaskPtr));
taskPtr.release();
} else {
this->SubmitTask(std::move(*taskPtr.release()));
}
}
}
return resultFuture;
}
} // NThread
#endif