Events¶
Events are (currently) manually triggerd/scheduled callbacks. There exist two event types of events, a delayed event and a timer event. Both event-types will run user-defined callbacks. Delayed events represent single-execution callbakcks while timers represent recurring actions.
Events are created and then must be registered on the reactor in order to be scheduled for execution. Once registered, they can be cancelled and they exist as RAII types, so destructing the object will also cause the callback to be de-registered from the reactor.
Example
1 2 3 4 5 6 7 | auto reactor = pembroke::reactor().build();
auto x = 0;
auto event = pembroke::event::TimerEvent(100us, [&]() -> void {
x += 1;
});
reactor->register_event(event);
|
If this event is not canceled or destructe******d it will run on the next iteration of the reactor’s event-loop and will continue to run every 100 microseconds.
TimerEvent¶
-
class
pembroke::event::TimerEvent: public pembroke::Event, public pembroke::EventCancellation¶ A repeating event that triggers every user-defined interval
Useful for creating repeating tasks such as maintenance/cleanup tasks, data-updates, metric/log flushing, etc. The timer will start as soon as it is registered with the reactor (or with a delay if specified) and will re-run at the specified interval after completion.
Example: If a timer is created to run every 5s and takes 2s to complete, scheduling will look like:
|**|-----|**|-----|**|-----|**|... * == 1s running - == 1s waiting
The 5s timer does not start until the callback completes. So in this scenario, the times at which a task starts may be 7s apart each time.
Public Functions
-
TimerEvent(duration interval, std::function<void()> callback)¶ Construct a Timer that executes
callbackimmediately and then everyinterval.
-
TimerEvent(duration initial_delay, duration interval, std::function<void()> callback)¶ Construct a timer that execute
callbackafter an initial duration ofinitial_delay` and then everyinterval``.
-
auto
cancel() noexcept -> bool override¶ Cancels the event. Callbacks on the event should not run after cancel has been called (assuming cancellation suceeds).
-
auto
canceled() noexcept -> bool override¶ Returns bool indicating if the event has been canceled.
- Note
Does not take into account event’s registration status.
-
DelayedEvent¶
-
class
pembroke::event::DelayedEvent: public pembroke::Event, public pembroke::EventCancellation¶ A one-time event that will execute after an initial delay. Represents the most basic event type.
Useful for defering actions, implementing timeout behaviors for asynchronous events, or building more complex event types.
Public Functions
-
DelayedEvent(duration delay, std::function<void()> callback)¶ Construct a Delayed event that executes
callbackafter an initial durationdelay.
-
auto
cancel() noexcept -> bool override¶ Cancels the event. Callbacks on the event should not run after cancel has been called (assuming cancellation suceeds).
-
auto
canceled() noexcept -> bool override¶ Returns bool indicating if the event has been canceled.
- Note
Does not take into account event’s registration status.
-