qtrocket/utils/ThreadPool.cpp
Travis Hunter e6bf1fea9b
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, gcc-13, g++-13, ubuntu-latest) (push) Has been cancelled
Revert "Merge pull request #20 from cthunter01/propagator"
This reverts commit 6280d9fb0184275843a8f4406c7293e41e65a639, reversing
changes made to 3c9c8b8c6a2b2e7430ff09efdc2cc0c1996b16ca.
2025-04-16 18:23:28 -06:00

55 lines
768 B
C++

/// \cond
// C headers
// C++ headers
#include <cstdint>
// 3rd party headers
/// \endcond
// qtrocket headers
#include "ThreadPool.h"
ThreadPool::ThreadPool()
: done(false),
q(),
threads(),
joiner(threads)
{
const std::size_t threadCount = std::thread::hardware_concurrency();
try
{
for(size_t i = 0; i < threadCount; ++i)
{
threads.push_back(std::thread(&ThreadPool::worker, this));
}
}
catch(...)
{
done = true;
throw;
}
}
ThreadPool::~ThreadPool()
{
done = true;
}
void ThreadPool::worker()
{
while(!done)
{
std::function<void()> task;
if(q.tryPop(task))
{
task();
}
else
{
std::this_thread::yield();
}
}
}