|
Fray Engine
|
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 |
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.
|
inline |
Queues a task to run on the queue's thread. Never waits for it to run.
| task | Work to run later. |
Definition at line 96 of file thread_manager.hpp.
|
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.
| keepAlive | Optional 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.
|
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.
|
private |
Guards _tasks and _shutdown. Tasks run with it released.
Definition at line 82 of file thread_manager.hpp.
|
private |
True once shutdown() was called; no new tasks are accepted.
Definition at line 88 of file thread_manager.hpp.
|
private |
Tasks waiting to run, oldest first.
Definition at line 86 of file thread_manager.hpp.
|
private |
Wakes run() when a task is posted or shutdown is requested.
Definition at line 84 of file thread_manager.hpp.