Class tasklet
Defined in File tasklet.h
Nested Relationships
Nested Types
Class Documentation
-
class tasklet
Tasklet class
The tasklet class aims to make thread management a little easier.
Specifically, it decouples the task to be executed from the lifetime of the thread, effectively enabling restartable threads. The task to be executed is handed to the tasklet as a task_function. Tasklet then adds start(), stop(), wakeup() and join() functions to it.
As oftentimes threads spend much of their time waiting for some external condition, tasklet also accepts a condition variable (and associated mutex). With these, a task_function can sleep until the condition has been met. If no external condition is provided, an internal one is used to facilitate sleeping.
stop() and wakeup() both signal the condition. If the condition is shared (provided from external), notify_all() is sent, as it cannot be determined which waiting thread should be woken.
Public Types
Public Functions
-
explicit tasklet(task_function &&func, bool start_now = false)
Create a tasklet either without a sleep_condition.
- Parameters
func – [in] Function to execute in the task.
start_now – [in] Start the task immediately.
-
tasklet(task_function &&func, sleep_condition *condition, bool start_now = false)
Create a tasklet either with a sleep_condition. The tasklet does not take onwership, and assumes the caller does.
- Parameters
func – [in] Function to execute in the task.
condition – [in] A pointer to a condition variable to sleep on instead of the default, built-in condition.
start_now – [in] Start the task immediately.
-
~tasklet()
-
bool start()
Starts the task.
- Returns
true if the tasklet was in a startable state and is now started.
-
bool stop()
Stops the task.
- Returns
true if the tasklet was in a stoppable state and is now stopped.
-
void wait()
Wait for the tasklet to terminate. This is synonymous to thread::join(); the call will block until tasklet is restartable.
-
void wakeup()
Wakes the thread up from sleeping on the condition variable.
-
struct context
The thread context. This is passed to the task function (below).
Public Functions
-
explicit context(sleep_condition *condition)
Constructor
- Parameters
condition – [in] optional sleep_condition.
-
virtual ~context()
Destructor
-
template<typename durationT>
inline bool sleep(durationT const &duration) const Sleep function. Use with std::chrono’s durations. Returns true if the tasklet is still supposed to be running (i.e. sleep was interrupted by wakeup() rather than stop()), so you can build your thread function like this:
void func(tasklet & t, void * baton) { while (t.sleep()) { // Do something } }
- Parameters
duration – [in] The duration for which to sleep, in any of the std::chrono units.
- Returns
true if the tasklet is still running, false otherwise.
Public Members
-
std::atomic<bool> running
Gets set or unset by tasklet functions. Can also be used in the task_function to determine whether to exit the thread loop.
-
sleep_condition *condition
Gets notified when the running flag changes. Wait on this condition variable to sleep in the thread loop.
-
explicit context(sleep_condition *condition)
-
struct sleep_condition
Bundle condition variable and mutex into one. This way, they cannot be provided separately.
-
explicit tasklet(task_function &&func, bool start_now = false)