libfly
6.2.2
C++20 utility library for Linux, macOS, and Windows
|
#include <task_manager.hpp>
Public Member Functions | |
bool | start () |
bool | stop () |
Static Public Member Functions | |
static std::shared_ptr< TaskManager > | create (std::uint32_t thread_count) |
Friends | |
class | TaskRunner |
Class to manage a pool of threads for executing tasks posted by any task runner. Also manages a timer thread to hold delayed tasks until their scheduled time.
The task manager makes no guarantee on the order of task execution; when a task is given to the task manager, it will be executed as soon as a worker thread is available. Instead, ordering is controlled by the task runners. A task runner may hold on to a task in accordance with its defined behavior until it is ready for the task manager to execute the task.
|
static |
Create and start a task manager.
thread_count | Size of the thread pool for the task manager to own. |
bool fly::task::TaskManager::start | ( | ) |
Create the worker threads and timer thread.
bool fly::task::TaskManager::stop | ( | ) |
Destroy the worker threads and timer thread, blocking until the threads exit. This must be eplicitly called from the same thread that started the task manager.
TODO: TaskManager should be created as a unique_ptr so it can be started and stopped in an RAII fashion. It cannot be stopped from the destructor because a task thread may be the last owner of a shared_ptr to this task manager. In that case, when that shared_ptr is destroyed, the destructor would call stop, resulting in the task thread trying to join itself.