9 #include <condition_variable> 23 using Job = std::function<void()>;
44 template<
typename Result>
45 std::future<Result>
add(std::function<Result()> job)
47 const auto promise = std::make_shared<std::promise<Result>>();
48 std::future<Result> future = promise->get_future();
50 promise->set_value(job());
59 void _thread_worker(
size_t thread_nr);
62 std::vector<std::thread> _threads;
63 std::deque<Job> _job_queue;
64 size_t _num_unfinished_jobs = 0;
65 std::condition_variable _new_job_cond;
66 std::condition_variable _job_finished_cond;
Definition: thread_pool.hpp:20
void wait()
Wait for all jobs to finish.
ThreadPool()
As many threads as cores, but at least 2.
~ThreadPool()
Will block until all jobs have finished.
void clear()
Remove all jobs in the queue (but those that have already started will still finish).
void add_void(const Job &job)
Add to queue and return immediately.
std::future< Result > add(std::function< Result()> job)
Add to queue and return immediately.
Definition: thread_pool.hpp:45
Definition: coroutine.hpp:18