Nameless Engine
Loading...
Searching...
No Matches
ne::ThreadPool Class Reference

#include <ThreadPool.h>

Public Member Functions

 ThreadPool ()
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (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
 

Detailed Description

A very simple thread pool.

Constructor & Destructor Documentation

◆ ThreadPool()

ne::ThreadPool::ThreadPool ( )

Creates threads to execute tasks.

◆ ~ThreadPool()

ne::ThreadPool::~ThreadPool ( )

Waits for all threads to stop.

Member Function Documentation

◆ addTask()

void ne::ThreadPool::addTask ( const std::function< void()> &  task)

Adds a new task to be executed in the thread pool.

Parameters
taskA task to add.

◆ processTasksThread()

void ne::ThreadPool::processTasksThread ( )
protected

Function that each thread is executing. Waits for new tasks and processes one.

◆ stop()

void ne::ThreadPool::stop ( )

Stop all working threads. Can be called explicitly. If not called explicitly will be called in destructor.

Member Data Documentation

◆ bIsShuttingDown

std::atomic_flag ne::ThreadPool::bIsShuttingDown
private

Atomic flag to set when destructor is called so that running threads are notified to finish.

◆ cvNewTasks

std::condition_variable ne::ThreadPool::cvNewTasks
private

Condition variable to wait until new tasks are added.

◆ iMinThreadCount

const unsigned int ne::ThreadPool::iMinThreadCount = 4
private

Minimum amount of threads to create when hardware concurrency information is not available.

◆ mtxTaskQueue

std::pair<std::mutex, std::queue<std::function<void()> > > ne::ThreadPool::mtxTaskQueue
private

Mutex for read/write operations on task queue. Task queue that contains all tasks to be executed in the thread pool.

◆ vRunningThreads

std::vector<std::thread> ne::ThreadPool::vRunningThreads
private

Array of running threads.


The documentation for this class was generated from the following files: