Sunshine master
Self-hosted game stream host for Moonlight.
thread_pool_util::ThreadPool Class Reference

#include <src/thread_pool.h>

Inheritance diagram for thread_pool_util::ThreadPool:
[legend]
Collaboration diagram for thread_pool_util::ThreadPool:
[legend]

Public Types

typedef TaskPool::__task __task
 Callable unit executed by a worker thread.
 
- Public Types inherited from task_pool_util::TaskPool
typedef std::unique_ptr< _ImplBase__task
 Type-erased task owned by the task pool.
 
typedef std::chrono::steady_clock::time_point __time_point
 Steady-clock timestamp used to schedule delayed tasks.
 
typedef _ImplBasetask_id_t
 Stable pointer used to identify a queued task.
 

Public Member Functions

void _main ()
 Run the main application or worker loop.
 
void join ()
 Wait for worker threads owned by the session to exit.
 
template<class Function , class... Args>
auto push (Function &&newTask, Args &&...args)
 Queue work for asynchronous execution.
 
template<class Function , class X , class Y , class... Args>
auto pushDelayed (Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
 Queue a task that becomes runnable after a delay.
 
void pushDelayed (std::pair< __time_point, __task > &&task)
 Queue a task that becomes runnable after a delay.
 
void start (int threads)
 Start worker threads for queued thread-pool tasks.
 
void stop ()
 Stop worker threads and prevent additional task execution.
 
 ThreadPool (int threads)
 Start a pool with the requested number of worker threads.
 
- Public Member Functions inherited from task_pool_util::TaskPool
bool cancel (task_id_t task_id)
 Cancel a queued or delayed task before it runs.
 
template<class X , class Y >
void delay (task_id_t task_id, std::chrono::duration< X, Y > duration)
 
std::optional< __time_pointnext ()
 Return the next timer task ready for execution.
 
TaskPooloperator= (TaskPool &&other) noexcept
 Assign state from another instance while preserving ownership semantics.
 
std::optional< __taskpop ()
 Remove and return the next queued item, waiting when requested.
 
std::optional< std::pair< __time_point, __task > > pop (task_id_t task_id)
 Remove and return the next queued item, waiting when requested.
 
template<class Function , class... Args>
auto push (Function &&newTask, Args &&...args)
 Queue work for asynchronous execution.
 
template<class Function , class X , class Y , class... Args>
auto pushDelayed (Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
 
void pushDelayed (std::pair< __time_point, __task > &&task)
 Queue a task that becomes runnable after a delay.
 
bool ready ()
 Check whether the task pool has active workers.
 
 TaskPool (TaskPool &&other) noexcept
 Move queued tasks and timers from another task pool.
 

Additional Inherited Members

- Protected Attributes inherited from task_pool_util::TaskPool
std::mutex _task_mutex
 Mutex protecting task queues and worker wakeups.
 
std::deque< __task_tasks
 Immediate tasks waiting for worker execution.
 
std::vector< std::pair< __time_point, __task > > _timer_tasks
 Delayed tasks sorted by their due time.
 

Detailed Description

Allow threads to execute unhindered while keeping full control over the threads.

Constructor & Destructor Documentation

◆ ThreadPool()

thread_pool_util::ThreadPool::ThreadPool ( int threads)
inlineexplicit

Start a pool with the requested number of worker threads.

Parameters
threadsNumber of worker threads to start.

Member Function Documentation

◆ push()

template<class Function , class... Args>
auto thread_pool_util::ThreadPool::push ( Function && newTask,
Args &&... args )
inline

Queue work for asynchronous execution.

Parameters
newTaskNew task.
argsArguments forwarded to the callable or parser.
Returns
Identifier assigned to the queued task.

◆ pushDelayed() [1/2]

template<class Function , class X , class Y , class... Args>
auto thread_pool_util::ThreadPool::pushDelayed ( Function && newTask,
std::chrono::duration< X, Y > duration,
Args &&... args )
inline

Queue a task that becomes runnable after a delay.

Parameters
newTaskNew task.
durationDelay before the timed task should run.
argsArguments forwarded to the callable or parser.
Returns
Future that becomes ready after the delayed task completes.

◆ pushDelayed() [2/2]

void thread_pool_util::ThreadPool::pushDelayed ( std::pair< __time_point, __task > && task)
inline

Queue a task that becomes runnable after a delay.

Parameters
taskTask object to enqueue or execute.

◆ start()

void thread_pool_util::ThreadPool::start ( int threads)
inline

Start worker threads for queued thread-pool tasks.

Parameters
threadsNumber of worker threads to start.

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