parallel wip
This commit is contained in:
188
threadpoool.h
188
threadpoool.h
@@ -4,129 +4,101 @@
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include <vector>
|
||||
|
||||
class thread_pool {
|
||||
public:
|
||||
thread_pool(std::size_t size) : stop(false), exit_on_empty(false) {
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
workers.emplace_back([this] { spawn(); });
|
||||
}
|
||||
}
|
||||
struct thread_pool {
|
||||
explicit thread_pool(std::size_t size) {
|
||||
start(size);
|
||||
finished.reserve(1024);
|
||||
}
|
||||
|
||||
virtual ~thread_pool() {
|
||||
if (!stop) join();
|
||||
}
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
std::deque<std::packaged_task<void()>> work;
|
||||
|
||||
// template<class F, class... Args>
|
||||
// void post(F&& f, Args&&... args) {
|
||||
void post(std::function<void()> f) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
tasks.push(f);
|
||||
}
|
||||
std::vector<std::future<void>> finished;
|
||||
|
||||
condition.notify_one();
|
||||
}
|
||||
// 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));
|
||||
|
||||
void join() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
stop = true;
|
||||
}
|
||||
auto r = p.get_future();
|
||||
{
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
work.emplace_back(std::move(p));
|
||||
}
|
||||
condition.notify_one();
|
||||
|
||||
condition.notify_all();
|
||||
return r; // return the future result of the task
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < workers.size(); ++i) {
|
||||
workers[i].join();
|
||||
}
|
||||
}
|
||||
// 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(); }
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void finish_tasks() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
exit_on_empty = true;
|
||||
}
|
||||
|
||||
while (!tasks.empty()) {
|
||||
condition.notify_all();
|
||||
// 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();
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
condition_empty.wait(lock, [this]() {
|
||||
return (tasks.empty());
|
||||
});
|
||||
}
|
||||
// cancel_pending() merely cancels all non-started tasks:
|
||||
void cancel_pending() {
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
work.clear();
|
||||
}
|
||||
|
||||
// BLEJU, BLEJU
|
||||
// while (!tasks.empty()) condition.notify_all();
|
||||
bool op = true;
|
||||
}
|
||||
// finish enques a "stop the thread" message for every thread, then waits for them:
|
||||
void finish() {
|
||||
{
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
for (auto &&unused:finished) {
|
||||
work.emplace_back();
|
||||
}
|
||||
}
|
||||
condition.notify_all();
|
||||
finished.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
void spawn() {
|
||||
std::function<void()> task;
|
||||
~thread_pool() {
|
||||
finish();
|
||||
}
|
||||
|
||||
while (!stop && (!exit_on_empty || !tasks.empty())) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
if (exit_on_empty) {
|
||||
condition_empty.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<std::thread> workers;
|
||||
std::queue<std::function<void()>> tasks;
|
||||
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
std::condition_variable condition_empty;
|
||||
bool stop;
|
||||
bool exit_on_empty;
|
||||
private:
|
||||
void thread_task() {
|
||||
while (true) {
|
||||
std::packaged_task<void()> f;
|
||||
{
|
||||
std::unique_lock<std::mutex> l(mutex);
|
||||
if (work.empty()) {
|
||||
condition.wait(l, [&] { return !work.empty(); });
|
||||
}
|
||||
f = std::move(work.front());
|
||||
work.pop_front();
|
||||
}
|
||||
// if the task is invalid, it means we are asked to abort:
|
||||
if (!f.valid()) return;
|
||||
|
||||
f();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// template<class F, class... Args>
|
||||
// inline void dispatch(thread_pool& pool, F&& f, Args&&... args) {
|
||||
inline void dispatch(thread_pool& pool, std::function<void()> f) {
|
||||
pool.post(f);
|
||||
inline void dispatch(thread_pool &pool, std::function<void()> f) {
|
||||
pool.queue(f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// int main() {
|
||||
// int cnt = 0;
|
||||
|
||||
// thread_pool tp{std::thread::hardware_concurrency()};
|
||||
// std::mutex mutex;
|
||||
|
||||
// std::cout << "start" << std::endl;
|
||||
|
||||
// for(int i=0; i<100; i++) {
|
||||
// dispatch(tp, std::function<void()>
|
||||
// ([i, &cnt, &mutex]() {
|
||||
// std::cout << "test " << i << std::endl;
|
||||
// {
|
||||
// std::unique_lock<std::mutex> lock(mutex);
|
||||
// cnt++;
|
||||
// }
|
||||
// }
|
||||
// ));
|
||||
// }
|
||||
|
||||
|
||||
// std::cout << "end" << std::endl;
|
||||
// tp.join();
|
||||
// std::cout << "cnt:" << cnt << std::endl;
|
||||
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user