Skip to content

EventLoop

class EventLoop

@brief Karabo's central event loop. Asynchronous events are passed throughout the distributed system by posting to the loop.

Functions

Name Description
addThread Add a number of threads to the event loop, increasing the number of thread available to handle events posted to the loop nThreads :
removeThread Remove a number of threads from the event loop, reducing the number of threads available to handle events posted to the loop nThreads :
post Post a task on the underlying io event loop for later execution func : a functor not taking any argument, but with any return type delayMs : execution will be delayed by given time (in milliseconds)
getIOService Return the Eventloop's underlying boost::asio::io_service Return :
work Start the event loop and block until EventLoop::stop() is called. The system signals SIGINT and SIGTERM will be caught and trigger the following actions: - a signal handler set via setSignalHandler is called, - and EventLoop::stop() is called. Must not be called in parallel to itself or to run(). If one or more tasks are in deadlock and thus their threads cannot be joined at the end, a karabo::data::TimeoutException is thrown.
run Start the event loop and block until all work posted to its io service is completed or until EventLoop::stop() is called. Must not be called in parallel to itself or to work(). If one or more tasks are in deadlock and thus their threads cannot be joined at the end, a karabo::data::TimeoutException is thrown.
stop Stop the event loop, canceling any remaining work, i.e. unblocking run()
getNumberOfThreads Return the number of threads currently available to the event loop for distributing work Return :
setSignalHandler Set the handler to be called if a system signal is caught.
setCatchExceptions Public flag to change behaviour of catching exceptions in threads By default, flag is true and the event loop runs its threads such that any exception is caught and logged as error.
clearThreadPool Clears the thread pool and joins the threads If joining fails repeatedly, throws karabo::data::TimeoutException.

Function Details

addThread

static void addThread(const int nThreads = 1)

Add a number of threads to the event loop, increasing the number of thread available to handle events posted to the loop

nThreads :

clearThreadPool

void clearThreadPool()

Clears the thread pool and joins the threads

If joining fails repeatedly, throws karabo::data::TimeoutException.

getIOService

static boost::asio::io_context& getIOService()

Return the Eventloop's underlying boost::asio::io_service

Return :

getNumberOfThreads

static size_t getNumberOfThreads()

Return the number of threads currently available to the event loop for distributing work

Return :

post

template <class Function> static void post(Function&& func, unsigned int delayMs = 0)

Post a task on the underlying io event loop for later execution

func : a functor not taking any argument, but with any return type

delayMs : execution will be delayed by given time (in milliseconds)

removeThread

static void removeThread(const int nThreads = 1)

Remove a number of threads from the event loop, reducing the number of threads available to handle events posted to the loop

nThreads :

run

static void run()

Start the event loop and block until all work posted to its io service is completed or until EventLoop::stop() is called.

Must not be called in parallel to itself or to work().

If one or more tasks are in deadlock and thus their threads cannot be joined at the end, a karabo::data::TimeoutException is thrown.

setCatchExceptions

static bool setCatchExceptions(bool flag)

Public flag to change behaviour of catching exceptions in threads

By default, flag is true and the event loop runs its threads such that any exception is caught and logged as error. If flag is false, exceptions are rethrown after logging them.

flag : that should be valid now

Return : previous value of flag

setSignalHandler

static void setSignalHandler(const SignalHandler& handler)

Set the handler to be called if a system signal is caught.

See work() about which signals are caught.

handler : function with signature 'void (int signal)'

stop

static void stop()

Stop the event loop, canceling any remaining work, i.e. unblocking run()

work

static void work()

Start the event loop and block until EventLoop::stop() is called.

The system signals SIGINT and SIGTERM will be caught and trigger the following actions: - a signal handler set via setSignalHandler is called, - and EventLoop::stop() is called.

Must not be called in parallel to itself or to run().

If one or more tasks are in deadlock and thus their threads cannot be joined at the end, a karabo::data::TimeoutException is thrown.