mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 14:43:34 +00:00
adding thread pool
This commit is contained in:
32
Application/ThreadPool.h
git.filemode.normal_file
32
Application/ThreadPool.h
git.filemode.normal_file
@@ -0,0 +1,32 @@
|
||||
#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;)
|
||||
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
|
||||
Reference in New Issue
Block a user