qtrocket/utils/ThreadPool.h
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

69 lines
1021 B
C++

#ifndef THREADPOOL_H
#define THREADPOOL_H
/// \cond
// C headers
// C++ headers
#include <atomic>
#include <functional>
#include <thread>
#include <vector>
// 3rd party headers
/// \endcond
// qtrocket headers
#include "utils/TSQueue.h"
/**
* @brief A basic ThreadPool class
*/
class ThreadPool
{
public:
ThreadPool();
~ThreadPool();
template<typename FunctionType>
void submit(FunctionType f)
{
q.push(std::function<void()>(f));
}
private:
class JoinThreads
{
public:
explicit JoinThreads(std::vector<std::thread>& inThreads)
: threads(inThreads)
{}
~JoinThreads()
{
for(auto& i : threads)
{
if(i.joinable())
{
i.join();
}
}
}
private:
std::vector<std::thread>& threads;
};
std::atomic_bool done;
TSQueue<std::function<void()>> q;
std::vector<std::thread> threads;
JoinThreads joiner;
void worker();
};
#endif // THREADPOOL_H