manticore
Main thread executor
Loading...
Searching...
No Matches
Public Member Functions | List of all members
manticore::Executor Class Reference

Execute arbitrary functions on the main thread. More...

#include <manticore.hpp>

Public Member Functions

void initialize (size_t n, std::string e)
 
void initialize (size_t n)
 
void finish_thread (bool notify=true)
 
template<class Function_ >
void run (Function_ f)
 
void listen ()
 

Detailed Description

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.

Member Function Documentation

◆ finish_thread()

void manticore::Executor::finish_thread ( bool  notify = true)
inline

Indicate that one of the worker threads has finished its execution. This is thread-safe and should be called by each worker thread upon completion.

Parameters
notifyWhether to notify the main thread that execution has finished on a worker thread. Set to false for more efficiency if this method is called before listen().

◆ initialize() [1/2]

void manticore::Executor::initialize ( size_t  n)
inline

Initialize a parallel execution session where some tasks must be run on the main thread.

Parameters
nNumber of worker threads in this session.

◆ initialize() [2/2]

void manticore::Executor::initialize ( size_t  n,
std::string  e 
)
inline

Initialize a parallel execution session where some tasks must be run on the main thread.

Parameters
nNumber of worker threads in this session.
eDefault error message if a non-standard exception is thrown.

◆ listen()

void manticore::Executor::listen ( )
inline

Listen for run() requests from worker threads to execute a function on the main thread. This should be run only on the main thread, and will exit once all worker threads have called finish_thread().

◆ run()

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
fFunction object or functor to be run on the main thread.

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