1#ifndef BVH_V2_THREAD_POOL_H
2#define BVH_V2_THREAD_POOL_H
6#include <condition_variable>
15 using Task = std::function<void(
size_t)>;
35 inline void start(
size_t);
50 std::unique_lock<std::mutex> lock(
mutex_);
51 tasks_.emplace(std::move(task));
57 std::unique_lock<std::mutex> lock(
mutex_);
65 std::unique_lock<std::mutex> lock(pool->
mutex_);
69 task = std::move(pool->
tasks_.front());
75 std::unique_lock<std::mutex> lock(pool->
mutex_);
78 pool->
done_.notify_one();
83 if (thread_count == 0)
84 thread_count = std::max(1u, std::thread::hardware_concurrency());
85 for (
size_t i = 0; i < thread_count; ++i)
91 std::unique_lock<std::mutex> lock(
mutex_);
std::vector< std::thread > threads_
static void worker(ThreadPool *, size_t)
size_t get_thread_count() const
std::queue< Task > tasks_
std::condition_variable avail_
std::condition_variable done_
ThreadPool(size_t thread_count=0)
Creates a thread pool with the given number of threads (a value of 0 tries to autodetect the number o...
std::function< void(size_t)> Task