Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | List of all members
ola::thread::ExecutorThread Class Reference

Detailed Description

Enables callbacks to be executed in a separate thread.

Parameters
callbackthe callback to run.

This can be used to deferred deletion of objects. e.g.

ExecutorThread cleanup_thread;
cleanup_thread.Start();
Foo *f = new Foo();
// Some time later ...
// f will be deleted in a separate thread.
cleanup_thread.Execute(DeletePointerCallback(f));
cleanup_thread.Stop()
Inheritance diagram for ola::thread::ExecutorThread:
Inheritance graph
[legend]
Collaboration diagram for ola::thread::ExecutorThread:
Collaboration graph
[legend]

Public Member Functions

 ExecutorThread (const ola::thread::Thread::Options &options)
 Create a new ExecutorThread. More...
 
void Execute (ola::BaseCallback0< void > *callback)
 Execute the supplied callback at some point in the future. More...
 
void DrainCallbacks ()
 Block until all pending callbacks have been processed. More...
 
bool Start ()
 Start the ExecutorThread. More...
 
bool Stop ()
 Stop this Executor thread. More...
 

Constructor & Destructor Documentation

ola::thread::ExecutorThread::ExecutorThread ( const ola::thread::Thread::Options options)
inlineexplicit

Create a new ExecutorThread.

Parameters
optionsThe thread options to use

Member Function Documentation

void ola::thread::ExecutorThread::DrainCallbacks ( )
virtual

Block until all pending callbacks have been processed.

The callbacks continue to be run in the ExecutorThread, so if the thread has been stopped this will block indefinitely.

Implements ola::thread::ExecutorInterface.

void ola::thread::ExecutorThread::Execute ( ola::BaseCallback0< void > *  callback)
virtual

Execute the supplied callback at some point in the future.

Parameters
callbackthe callback to run.

This method provides the following guarantees:

  • The callback will not be run immediately.
  • The callback will be run at some point in the future. That is, the callback will not leak. Any remaining pending callbacks will be run during the destruction of the class implementing ExecutorInterface.
  • For a given thread, callbacks will be run in the order in which they were added.

When queuing callbacks, you need to ensure that either:

  • The objects used in the callback outlive the ExecutorInterface
  • That the callback is run before the objects are deleted.

To achieve the latter it's common to keep track of the number of outstanding callbacks and then call DrainCallbacks() in the destructor if the number of outstanding callbacks is non-0.

Implements ola::thread::ExecutorInterface.

bool ola::thread::ExecutorThread::Start ( )

Start the ExecutorThread.

Returns
true if the thread started, false if the thread was already running.

Not thread safe, should only be called once.

bool ola::thread::ExecutorThread::Stop ( )

Stop this Executor thread.

Returns
true if the Executor was stopped, false if it wasn't running.

Stop() will halt the executor thread, and process any pending callbacks in the current thread. Stop() will return once there is no pending callbacks.

Not thread safe, should only be called once.


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