Fray Engine
Loading...
Searching...
No Matches
fray::threads::TaskQueue Class Reference

A task loop that runs on one thread. Other threads post work to it. Tasks run one at a time in posting order. post() and shutdown() are safe from any thread; run() belongs to the one thread that drains it. More...

#include <thread_manager.hpp>

Public Member Functions

bool post (std::function< void()> task)
 Queues a task to run on the queue's thread. Never waits for it to run.
void shutdown ()
 Stops accepting new tasks; run() returns once the queue is empty (and keepAlive is false). Safe from any thread.
void run (const std::function< bool()> &keepAlive=nullptr)
 Runs queued tasks until shutdown() was called, the queue is empty and keepAlive() is false. Call on the thread that should execute the tasks. Exceptions thrown by a task are logged and do not stop the loop.

Private Attributes

std::mutex _mutex
std::condition_variable _wake
std::deque< std::function< void()> > _tasks
bool _shutdown = false

Detailed Description

A task loop that runs on one thread. Other threads post work to it. Tasks run one at a time in posting order. post() and shutdown() are safe from any thread; run() belongs to the one thread that drains it.

Definition at line 78 of file thread_manager.hpp.

Member Function Documentation

◆ post()

bool fray::threads::TaskQueue::post ( std::function< void()> task)
inline

Queues a task to run on the queue's thread. Never waits for it to run.

Parameters
taskWork to run later.
Returns
false (and drops the task) once the queue has been shut down; true if queued.

Definition at line 96 of file thread_manager.hpp.

◆ run()

void fray::threads::TaskQueue::run ( const std::function< bool()> & keepAlive = nullptr)
inline

Runs queued tasks until shutdown() was called, the queue is empty and keepAlive() is false. Call on the thread that should execute the tasks. Exceptions thrown by a task are logged and do not stop the loop.

Parameters
keepAliveOptional predicate; while it returns true the loop keeps running even after shutdown(). Evaluated under the queue's mutex.

Definition at line 124 of file thread_manager.hpp.

◆ shutdown()

void fray::threads::TaskQueue::shutdown ( )
inline

Stops accepting new tasks; run() returns once the queue is empty (and keepAlive is false). Safe from any thread.

Definition at line 110 of file thread_manager.hpp.

Member Data Documentation

◆ _mutex

std::mutex fray::threads::TaskQueue::_mutex
private

Guards _tasks and _shutdown. Tasks run with it released.

Definition at line 82 of file thread_manager.hpp.

◆ _shutdown

bool fray::threads::TaskQueue::_shutdown = false
private

True once shutdown() was called; no new tasks are accepted.

Definition at line 88 of file thread_manager.hpp.

◆ _tasks

std::deque<std::function<void()> > fray::threads::TaskQueue::_tasks
private

Tasks waiting to run, oldest first.

Definition at line 86 of file thread_manager.hpp.

◆ _wake

std::condition_variable fray::threads::TaskQueue::_wake
private

Wakes run() when a task is posted or shutdown is requested.

Definition at line 84 of file thread_manager.hpp.


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