psemek/libs/util/source/threadpool.cpp

35 lines
480 B
C++

#include <psemek/util/threadpool.hpp>
namespace psemek::util
{
void threadpool::start(std::size_t thread_count)
{
for (std::size_t th = 0; th < thread_count; ++th)
{
threads.emplace_back([this]
{
while (true)
{
auto task = tasks_queue.pop();
if (!task)
break;
task();
}
});
}
}
void threadpool::stop()
{
tasks_queue.clear();
for (auto const & thread: threads)
{
tasks_queue.push({});
}
threads.clear();
}
}