Nameless Engine
|
#include <ThreadPool.h>
Public Member Functions | |
ThreadPool () | |
ThreadPool (const ThreadPool &)=delete | |
ThreadPool & | operator= (const ThreadPool &)=delete |
~ThreadPool () | |
void | addTask (const std::function< void()> &task) |
void | stop () |
Protected Member Functions | |
void | processTasksThread () |
Private Attributes | |
std::condition_variable | cvNewTasks |
std::vector< std::thread > | vRunningThreads |
std::pair< std::mutex, std::queue< std::function< void()> > > | mtxTaskQueue |
std::atomic_flag | bIsShuttingDown |
const unsigned int | iMinThreadCount = 4 |
A very simple thread pool.
ne::ThreadPool::ThreadPool | ( | ) |
Creates threads to execute tasks.
ne::ThreadPool::~ThreadPool | ( | ) |
Waits for all threads to stop.
void ne::ThreadPool::addTask | ( | const std::function< void()> & | task | ) |
Adds a new task to be executed in the thread pool.
task | A task to add. |
|
protected |
Function that each thread is executing. Waits for new tasks and processes one.
void ne::ThreadPool::stop | ( | ) |
Stop all working threads. Can be called explicitly. If not called explicitly will be called in destructor.
|
private |
Atomic flag to set when destructor is called so that running threads are notified to finish.
|
private |
Condition variable to wait until new tasks are added.
|
private |
Minimum amount of threads to create when hardware concurrency information is not available.
|
private |
Mutex for read/write operations on task queue. Task queue that contains all tasks to be executed in the thread pool.
|
private |
Array of running threads.