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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
* ThreadPool.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
#include "BlockingQueue.h"
#include <boost/thread/future.hpp>
#include <boost/thread/condition_variable.hpp>
VCMI_LIB_NAMESPACE_BEGIN
typedef std::function<void()> TRMGfunction ;
typedef std::optional<TRMGfunction> TRMGJob;
//Credit to https://github.com/Liam0205/toy-threadpool/tree/master/yuuki
class DLL_LINKAGE ThreadPool
{
private:
using Lock = boost::unique_lock<boost::shared_mutex>;
mutable boost::shared_mutex mx;
mutable boost::condition_variable_any cv;
mutable boost::once_flag once;
bool isInitialized = false;
bool stopping = false;
bool canceling = false;
public:
ThreadPool();
~ThreadPool();
void init(size_t numThreads);
void spawn();
void terminate();
void cancel();
public:
bool initialized() const;
bool running() const;
int size() const;
private:
bool isRunning() const;
public:
auto async(std::function<void()>&& f) const -> boost::future<void>;
private:
std::vector<boost::thread> workers;
mutable BlockingQueue<TRMGfunction> tasks;
};
ThreadPool::ThreadPool() :
once(BOOST_ONCE_INIT)
{};
ThreadPool::~ThreadPool()
{
terminate();
}
inline void ThreadPool::init(size_t numThreads)
{
boost::call_once(once, [this, numThreads]()
{
Lock lock(mx);
stopping = false;
canceling = false;
workers.reserve(numThreads);
for (size_t i = 0; i < numThreads; ++i)
{
workers.emplace_back(std::bind(&ThreadPool::spawn, this));
}
isInitialized = true;
});
}
bool ThreadPool::isRunning() const
{
return isInitialized && !stopping && !canceling;
}
inline bool ThreadPool::initialized() const
{
Lock lock(mx);
return isInitialized;
}
inline bool ThreadPool::running() const
{
Lock lock(mx);
return isRunning();
}
inline int ThreadPool::size() const
{
Lock lock(mx);
return workers.size();
}
inline void ThreadPool::spawn()
{
while(true)
{
bool pop = false;
TRMGfunction task;
{
Lock lock(mx);
cv.wait(lock, [this, &pop, &task]
{
pop = tasks.pop(task);
return canceling || stopping || pop;
});
}
if (canceling || (stopping && !pop))
{
return;
}
task();
}
}
inline void ThreadPool::terminate()
{
{
Lock lock(mx);
if (isRunning())
{
stopping = true;
}
else
{
return;
}
}
cv.notify_all();
for (auto& worker : workers)
{
worker.join();
}
}
inline void ThreadPool::cancel()
{
{
Lock lock(mx);
if (running())
{
canceling = true;
}
else
{
return;
}
}
tasks.clear();
cv.notify_all();
for (auto& worker : workers)
{
worker.join();
}
}
auto ThreadPool::async(std::function<void()>&& f) const -> boost::future<void>
{
using TaskT = boost::packaged_task<void>;
{
Lock lock(mx);
if (stopping || canceling)
{
throw std::runtime_error("Delegating task to a threadpool that has been terminated or canceled.");
}
}
auto task = std::make_shared<TaskT>(f);
boost::future<void> fut = task->get_future();
tasks.emplace([task]() -> void
{
(*task)();
});
cv.notify_one();
return fut;
}
VCMI_LIB_NAMESPACE_END
|