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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// Copyright (c) 2020 Robert Vaser
// Combination of ThreadPool implementation by progschj and
// task stealing by Sean Parent
#ifndef THREAD_POOL_THREAD_POOL_HPP_
#define THREAD_POOL_THREAD_POOL_HPP_
#include <algorithm>
#include <atomic>
#include <functional>
#include <future> // NOLINT
#include <memory>
#include <queue>
#include <thread> // NOLINT
#include <unordered_map>
#include <utility>
#include <vector>
namespace thread_pool {
class ThreadPool {
public:
explicit ThreadPool(
std::size_t num_threads = std::thread::hardware_concurrency())
: threads_(),
thread_map_(),
queues_(std::max(size_t(1), num_threads)),
task_id_(0) {
for (std::size_t i = 0; i != queues_.size(); ++i) {
threads_.emplace_back([this, i] () -> void { Task(i); });
thread_map_.emplace(threads_.back().get_id(), i);
}
}
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
ThreadPool(ThreadPool&&) = delete;
ThreadPool& operator=(ThreadPool&&) = delete;
~ThreadPool() {
for (auto& it : queues_) {
it.Done();
}
for (auto& it : threads_) {
it.join();
}
}
std::size_t num_threads() const {
return threads_.size();
}
const std::unordered_map<std::thread::id, std::size_t>& thread_map() const {
return thread_map_;
}
template<typename T, typename... Ts>
auto Submit(T&& routine, Ts&&... params)
-> std::future<typename std::result_of<T(Ts...)>::type> {
auto task = std::make_shared<std::packaged_task<typename std::result_of<T(Ts...)>::type()>>( // NOLINT
std::bind(std::forward<T>(routine), std::forward<Ts>(params)...));
auto task_result = task->get_future();
auto task_wrapper = [task] () {
(*task)();
};
auto task_id = task_id_++;
bool is_submitted = false;
for (std::size_t i = 0; i != queues_.size() * 42; ++i) {
if (queues_[(task_id + i) % queues_.size()].TryPush(task_wrapper)) {
is_submitted = true;
break;
}
}
if (!is_submitted) {
queues_[task_id % queues_.size()].Push(task_wrapper);
}
return task_result;
}
private:
void Task(std::size_t thread_id) {
while (true) {
std::function<void()> task;
for (std::size_t i = 0; i != queues_.size(); ++i) {
if (queues_[(thread_id + i) % queues_.size()].TryPop(&task)) {
break;
}
}
if (!task && !queues_[thread_id].Pop(&task)) {
break;
}
task();
}
}
struct TaskQueue {
public:
template<typename F>
void Push(F&& f) {
{
std::unique_lock<std::mutex> lock(mutex);
queue.emplace(std::forward<F>(f));
}
is_ready.notify_one();
}
bool Pop(std::function<void()>* f) {
std::unique_lock<std::mutex> lock(mutex);
while (queue.empty() && !is_done) {
is_ready.wait(lock);
}
if (queue.empty()) {
return false;
}
*f = std::move(queue.front());
queue.pop();
return true;
}
template<typename F>
bool TryPush(F&& f) {
{
std::unique_lock<std::mutex> lock(mutex, std::try_to_lock);
if (!lock) {
return false;
}
queue.emplace(std::forward<F>(f));
}
is_ready.notify_one();
return true;
}
bool TryPop(std::function<void()>* f) {
std::unique_lock<std::mutex> lock(mutex, std::try_to_lock);
if (!lock || queue.empty()) {
return false;
}
*f = std::move(queue.front());
queue.pop();
return true;
}
void Done() {
{
std::unique_lock<std::mutex> lock(mutex);
is_done = true;
}
is_ready.notify_all();
}
std::queue<std::function<void()>> queue;
std::mutex mutex;
std::condition_variable is_ready;
bool is_done = false;
};
std::vector<std::thread> threads_;
std::unordered_map<std::thread::id, std::size_t> thread_map_;
std::vector<TaskQueue> queues_;
std::atomic<std::size_t> task_id_;
};
} // namespace thread_pool
#endif // THREAD_POOL_THREAD_POOL_HPP_
|