Executing functions on the event loop
If you need to execute a function in the context of the event loop (for example to execute it synchronously with any callbacks or data reception) you can either use a signal or a timer. A signal will be executed once, while a timer will be executed periodically.
Signal
Signals can be created using the exec function. It
takes a callback which is going to be executed in the context of the event loop once picked up by
the event loop. The callback takes no
arguments and returns void.
-
virtual void FelixClientThreadInterface::exec(const UserFunction &user_function) = 0
-
typedef std::function<void()> FelixClientThreadInterface::UserFunction
Timer
Timers are sightly more involved: First you set the callback using the
callback_on_user_timer function,
then you can start the timer using the
user_timer_start
function and stop it using the user_timer_stop
function. The callback takes no arguments and returns void. Only one timer can be set at a time.
-
virtual void FelixClientThreadExtension423::callback_on_user_timer(OnUserTimerCallback on_user_timer_cb) = 0
-
virtual void FelixClientThreadExtension520::user_timer_start(std::chrono::milliseconds interval) = 0
-
virtual void FelixClientThreadExtension423::user_timer_stop() = 0
-
typedef std::function<void()> FelixClientThreadInterface::OnUserTimerCallback
Deprecated API
Calling user_timer_start
with an integer value is deprecated. Use the overload taking a std::chrono::milliseconds instead.
Example
1// Create a signal
2client.exec([]() { std::println("Hello from the event loop!"); });
3
4// Create a timer
5client.callback_on_user_timer([]() { std::println("I should be printed every second!");});
6client.user_timer_start(1000ms);