psemek/libs/util/source/threadpool.cpp

41 lines
657 B
C++

#include <psemek/util/threadpool.hpp>
#include <psemek/util/unused.hpp>
#include <psemek/util/to_string.hpp>
#include <psemek/log/log.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, th]
{
log::register_thread(to_string(name, '#', th));
while (true)
{
auto task = tasks_queue.pop();
if (!task)
break;
task();
}
});
}
}
void threadpool::stop()
{
tasks_queue.clear();
for (auto const & thread: threads)
{
unused(thread);
tasks_queue.push({});
}
threads.clear();
}
}