class thread_pool { std::atomic_bool done; thread_safe_queue> work_queue; std::vector threads; join_threads joiner; void worker_thread() { while (!done) { std::function task; if (work_queue.try_pop(task)) { task(); } else { std::this_thread::yield(); } } } public: thread_pool() : done(false), joiner(threads) { unsigned const thread_count = std::thread::hardware_concurrency(); try { for (unsigned i = 0; i < thread_count; ++i) { threads.push_back(std::thread(&thread_pool::worker_thread, this)); } } catch (...) { done = true; throw; } } ~thread_pool() { done = true; } template void submit(FunctionType f) { work_queue.push(std::function(f)); } };