Execute arbitrary functions on the main thread.
An instance of this class should be created on the main thread and initialized with initialize(). A corresponding number of worker threads can then be created using std::thread. Each worker can request functions for main thread execution via run(). The main thread should call listen() to wait for such requests, blocking until all workers have finished by calling finish_thread().
Outside of parallel contexts, the same class can be used to execute functions directly on the main thread by just calling run(). It is not necessary to call initialize() beforehand, nor is it required to listen(), or to call finish_thread(). This is helpful to enable the same code to be used in parallel and serial contexts.
template<class Function_ >
| void manticore::Executor::run |
( |
Function_ | f | ) |
|
|
inline |
Make a request to the main thread to run the specified function.
If initialize() was previously called on the main thread, this method should be called from a worker thread. It is expected that the main thread is (or will be) running listen().
If initialize() was not previously called, it is assumed that this method is being called from the main thread. In such cases, f is executed immediately.
- Template Parameters
-
| Function_ | Class of the function object, typically a lambda. This should accept no arguments and return no value. |
- Parameters
-
| f | Function object or functor to be run on the main thread. |