thread pool changed
This commit is contained in:
125
threadpool.h
125
threadpool.h
@@ -1,104 +1,79 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include <vector>
|
||||
|
||||
struct thread_pool {
|
||||
explicit thread_pool(std::size_t size) {
|
||||
start(size);
|
||||
finished.reserve(1024);
|
||||
class thread_pool {
|
||||
public:
|
||||
thread_pool(std::size_t size) : stop(false) {
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
workers.emplace_back([this] { spawn(); });
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
std::deque<std::packaged_task<void()>> work;
|
||||
virtual ~thread_pool() {
|
||||
if (!stop) join();
|
||||
}
|
||||
|
||||
std::vector<std::future<void>> finished;
|
||||
|
||||
// queue( lambda ) will enqueue the lambda into the tasks for the threads
|
||||
template<class F, class R=std::result_of_t<F &()>>
|
||||
std::future<R> queue(F &&f) {
|
||||
// wrap the function object into a packaged task, splitting
|
||||
// execution from the return value:
|
||||
std::packaged_task<R()> p(std::forward<F>(f));
|
||||
|
||||
auto r = p.get_future();
|
||||
void post(std::function<void()> f) {
|
||||
{
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
work.emplace_back(std::move(p));
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
tasks.push(f);
|
||||
}
|
||||
|
||||
condition.notify_one();
|
||||
|
||||
return r; // return the future result of the task
|
||||
}
|
||||
|
||||
// start threads_num threads in the thread pool.
|
||||
void start(std::size_t threads_num = 1) {
|
||||
for (std::size_t i = 0; i < threads_num; ++i) {
|
||||
finished.push_back(
|
||||
std::async(
|
||||
std::launch::async,
|
||||
[this] { thread_task(); }
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// abort() cancels all non-started tasks, and tells every working thread
|
||||
// stop running, and waits for them to finish up.
|
||||
void abort() {
|
||||
cancel_pending();
|
||||
finish();
|
||||
}
|
||||
|
||||
// cancel_pending() merely cancels all non-started tasks:
|
||||
void cancel_pending() {
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
work.clear();
|
||||
}
|
||||
|
||||
// finish enques a "stop the thread" message for every thread, then waits for them:
|
||||
void finish() {
|
||||
void join() {
|
||||
{
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
for (auto &&unused:finished) {
|
||||
work.emplace_back();
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
stop = true;
|
||||
}
|
||||
condition.notify_all();
|
||||
finished.clear();
|
||||
}
|
||||
|
||||
~thread_pool() {
|
||||
finish();
|
||||
condition.notify_all();
|
||||
|
||||
for (std::size_t i = 0; i < workers.size(); ++i) {
|
||||
workers[i].join();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void thread_task() {
|
||||
while (true) {
|
||||
std::packaged_task<void()> f;
|
||||
void spawn() {
|
||||
std::function<void()> task;
|
||||
|
||||
bool task_queue_empty = tasks.empty();
|
||||
while (!stop || !task_queue_empty) {
|
||||
bool task_valid = false;
|
||||
{
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
if (work.empty()) {
|
||||
condition.wait(l, [&] { return !work.empty(); });
|
||||
}
|
||||
f = std::move(work.front());
|
||||
work.pop_front();
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
condition.wait(lock, [this]() {
|
||||
return (!tasks.empty()) || (tasks.empty() && stop);
|
||||
});
|
||||
|
||||
if (!tasks.empty()) {
|
||||
task = std::move(tasks.front());
|
||||
tasks.pop();
|
||||
task_valid = true;
|
||||
}
|
||||
|
||||
task_queue_empty = tasks.empty();
|
||||
}
|
||||
// if the task is invalid, it means we are asked to abort:
|
||||
if (!f.valid()) return;
|
||||
|
||||
f();
|
||||
|
||||
if (task_valid) task();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<std::thread> workers;
|
||||
std::queue<std::function<void()>> tasks;
|
||||
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
bool stop;
|
||||
};
|
||||
|
||||
inline void dispatch(thread_pool &pool, std::function<void()> f) {
|
||||
pool.queue(f);
|
||||
pool.post(f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user