co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
co_ecs::thread_pool Class Reference

Generic thread pool implementation. More...

#include <thread_pool.hpp>

Classes

class  worker
 Thread pool worker. More...
 

Public Types

using thread_t = std::thread
 

Public Member Functions

 thread_pool (std::size_t num_workers=std::thread::hardware_concurrency())
 Construct thread pool with num_workers workers.
 
 ~thread_pool ()
 Destroy thread pool, worker threads are notified to exit and joined.
 
 thread_pool (const thread_pool &)=delete
 
thread_pooloperator= (const thread_pool &)=delete
 
 thread_pool (thread_pool &&)=delete
 
thread_pooloperator= (thread_pool &&)=delete
 
task_tsubmit (auto &&func, task_t *parent=nullptr)
 Submit a task to a thread pool.
 
void wait (task_t *task)
 Wait a task to complete.
 
workerget_worker_by_id (std::size_t id) noexcept
 Get worker by ID.
 
std::size_t num_workers () const noexcept
 Return the number of workers.
 

Static Public Member Functions

static thread_poolget ()
 Get thread pool instance.
 
static workercurrent_worker () noexcept
 Get current worker.
 

Detailed Description

Generic thread pool implementation.

Creates N worker threads. Each thread has its own local task queue that it can push/pop task items to/from. Once there's no tasks in local queue a worker thread tries to steal a task from a random worker.

Member Typedef Documentation

◆ thread_t

using co_ecs::thread_pool::thread_t = std::thread

Constructor & Destructor Documentation

◆ thread_pool() [1/3]

co_ecs::thread_pool::thread_pool ( std::size_t num_workers = std::thread::hardware_concurrency())
inline

Construct thread pool with num_workers workers.

Parameters
num_workersThe number of workers to create

◆ ~thread_pool()

co_ecs::thread_pool::~thread_pool ( )
inline

Destroy thread pool, worker threads are notified to exit and joined.

◆ thread_pool() [2/3]

co_ecs::thread_pool::thread_pool ( const thread_pool & )
delete

◆ thread_pool() [3/3]

co_ecs::thread_pool::thread_pool ( thread_pool && )
delete

Member Function Documentation

◆ current_worker()

static worker & co_ecs::thread_pool::current_worker ( )
inlinestaticnoexcept

Get current worker.

Returns
Worker

◆ get()

static thread_pool & co_ecs::thread_pool::get ( )
inlinestatic

Get thread pool instance.

Returns
thread_pool

◆ get_worker_by_id()

worker & co_ecs::thread_pool::get_worker_by_id ( std::size_t id)
inlinenoexcept

Get worker by ID.

Parameters
idWorker ID
Returns
Worker

◆ num_workers()

std::size_t co_ecs::thread_pool::num_workers ( ) const
inlinenoexcept

Return the number of workers.

Returns
Number of workers

◆ operator=() [1/2]

thread_pool & co_ecs::thread_pool::operator= ( const thread_pool & )
delete

◆ operator=() [2/2]

thread_pool & co_ecs::thread_pool::operator= ( thread_pool && )
delete

◆ submit()

task_t * co_ecs::thread_pool::submit ( auto && func,
task_t * parent = nullptr )
inline

Submit a task to a thread pool.

Parameters
funcFunction
parentParent task pointer

◆ wait()

void co_ecs::thread_pool::wait ( task_t * task)
inline

Wait a task to complete.

Parameters
task

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