Open Lighting Architecture: ola::thread::ExecutorThread Class Reference

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
Public Member Functions | List of all members
ola::thread::ExecutorThread Class Reference

## Detailed Description

Enables callbacks to be executed in a separate thread.

Parameters

  callback | the 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()
ola::thread::ExecutorThread::ExecutorThread
ExecutorThread(const ola::thread::Thread::Options &options)
Create a new ExecutorThread.
Definition ExecutorThread.h:60
ola::thread::ExecutorThread::Start
bool Start()
Start the ExecutorThread.
Definition ExecutorThread.cpp:60
ola::thread::ExecutorThread::Stop
bool Stop()
Stop this Executor thread.
Definition ExecutorThread.cpp:64
ola::thread::ExecutorThread::Execute
void Execute(ola::BaseCallback0< void > *callback)
Execute the supplied callback at some point in the future.
Definition ExecutorThread.cpp:46
ola::DeletePointerCallback
SingleUseCallback0< void > * DeletePointerCallback(T *t)
Create a callback that deletes the object.
Definition Deleter.h:45
Inheritance diagram for ola::thread::ExecutorThread:

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

## Constructor & Destructor Documentation

## ◆ ExecutorThread()

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

Create a new ExecutorThread.

Parameters

  options | The thread options to use

## Member Function Documentation

## ◆ DrainCallbacks()

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.

## ◆ Execute()

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

Execute the supplied callback at some point in the future.

Parameters

  callback | the 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.

## ◆ Start()

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.

## ◆ Stop()

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:

- include/ola/thread/ExecutorThread.h
- common/thread/ExecutorThread.cpp