mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-10 22:23:35 +00:00
33 lines
725 B
C++
33 lines
725 B
C++
#ifndef THREAD_POOL_H
|
|
#define THREAD_POOL_H
|
|
|
|
#include <thread>
|
|
#include <vector>
|
|
#include <functional>
|
|
#include <Utils.h>
|
|
|
|
constexpr std::uint32_t DEFAULT_CONCURENCY = 6;
|
|
|
|
class ThreadPool
|
|
{
|
|
public:
|
|
ThreadPool();
|
|
inline void EnqueueTask(std::function<void()> task) { m_tasks.Push(std::move(task)); }
|
|
inline void WaitForAllTasks() const
|
|
{
|
|
using namespace std::chrono_literals;
|
|
for(; m_waiting_count != m_concurency && !m_tasks.IsEmpty();)
|
|
std::this_thread::sleep_for(10ms);
|
|
}
|
|
~ThreadPool();
|
|
|
|
private:
|
|
ThreadSafeQueue<std::function<void()>> m_tasks;
|
|
std::vector<std::thread> m_pool;
|
|
std::uint32_t m_concurency;
|
|
std::atomic_uint32_t m_waiting_count;
|
|
std::atomic_bool m_stop = false;
|
|
};
|
|
|
|
#endif
|