Nameless Engine
Loading...
Searching...
No Matches
ThreadPool.h
1#pragma once
2
3// Standard.
4#include <atomic>
5#include <functional>
6#include <mutex>
7#include <queue>
8#include <condition_variable>
9
10namespace ne {
14 class ThreadPool {
15 public:
17 ThreadPool();
18
19 ThreadPool(const ThreadPool&) = delete;
20 ThreadPool& operator=(const ThreadPool&) = delete;
21
24
30 void addTask(const std::function<void()>& task);
31
37 void stop();
38
39 protected:
44 void processTasksThread();
45
46 private:
48 std::condition_variable cvNewTasks;
49
51 std::vector<std::thread> vRunningThreads;
52
57 std::pair<std::mutex, std::queue<std::function<void()>>> mtxTaskQueue;
58
63 std::atomic_flag bIsShuttingDown;
64
66 const unsigned int iMinThreadCount = 4;
67 };
68} // namespace ne
Definition: ThreadPool.h:14
std::pair< std::mutex, std::queue< std::function< void()> > > mtxTaskQueue
Definition: ThreadPool.h:57
std::vector< std::thread > vRunningThreads
Definition: ThreadPool.h:51
ThreadPool()
Definition: ThreadPool.cpp:11
std::atomic_flag bIsShuttingDown
Definition: ThreadPool.h:63
void stop()
Definition: ThreadPool.cpp:61
std::condition_variable cvNewTasks
Definition: ThreadPool.h:48
~ThreadPool()
Definition: ThreadPool.cpp:77
void processTasksThread()
Definition: ThreadPool.cpp:27
const unsigned int iMinThreadCount
Definition: ThreadPool.h:66
void addTask(const std::function< void()> &task)
Definition: ThreadPool.cpp:50