CppZmqZoltanExt 0.0.1
Loading...
Searching...
No Matches
zmqzext::loop_t Class Reference

Event loop for managing socket and timer events. More...

#include <loop.h>

Public Member Functions

void add (zmq::socket_ref socket, fn_socket_handler_t fn)
 Register a socket with an I/O handler.
 
timer_id_t add_timer (std::chrono::milliseconds timeout, std::size_t occurences, fn_timer_handler_t fn)
 Register a timer with an expiration handler.
 
void remove (zmq::socket_ref socket)
 Unregister a socket from the event loop.
 
void remove_timer (timer_id_t timer_id)
 Unregister a timer from the event loop.
 
void run (bool interruptible=true, std::chrono::milliseconds interruptCheckInterval=std::chrono::milliseconds{-1})
 Run the event loop.
 
bool terminated () const noexcept
 Check if the event loop has been terminated by interrupt signal or context termination.
 

Detailed Description

Event loop for managing socket and timer events.

The loop_t class provides a reactive event loop that monitors multiple sockets for I/O readiness and manages scheduled timers. It uses a poller_t internally to efficiently monitor multiple sockets simultaneously, and maintains a collection of timers with expiration tracking.

The event loop integrates with the interrupt handling system, allowing graceful shutdown in response to signals like SIGINT or SIGTERM.

Note
The loop runs in the calling thread and blocks until terminated.
This class is not thread-safe.
Interruptible behavior: when disabled, the loop ignores interrupt signals and continues running. This behavior may be desirable on actors when they should continue processing all events before receiving a stop request from the main application. So the main application can perform a graceful shutdown without the actors loosing any messages that are already in their queues.
InterruptCheckInterval: On Windows, the waiting calls to ZMQ functions do not return early on interrupt signals. Then, on an interrupt signal arrival, the loop would keep blocked indefinitely unless a socket becomes ready or a timer expires. Setting a finite InterruptCheckInterval allows the loop to periodically wake up and check the interrupt flag, enabling timely shutdown even on Windows. It is important to set this interval to a reasonable value, and also to set appropriate timeouts on all ZMQ calls (send and receive).
Interrupt checking requires install_interrupt_handler() to be called
See also
poller_t
install_interrupt_handler()

Definition at line 134 of file loop.h.

Member Function Documentation

◆ add()

void zmqzext::loop_t::add ( zmq::socket_ref  socket,
fn_socket_handler_t  fn 
)

Register a socket with an I/O handler.

Adds a socket to the event loop with an associated callback function. The callback is invoked whenever the socket becomes ready for receiving (data available to receive).

Parameters
socketThe ZMQ socket to register
fnCallback function to invoke when socket is ready
Exceptions
std::invalid_argumentif the socket is invalid or already added
See also
remove()

Definition at line 42 of file loop.cpp.

◆ add_timer()

timer_id_t zmqzext::loop_t::add_timer ( std::chrono::milliseconds  timeout,
std::size_t  occurences,
fn_timer_handler_t  fn 
)

Register a timer with an expiration handler.

Adds a timer to the event loop that will expire at regular intervals. The callback is invoked when the timer expires. Timers can be one-shot (occurences=1) or recurring (occurences > 1 or 0 for infinite).

Parameters
timeoutDuration between timer expirations in milliseconds
occurencesNumber of times the timer should fire (0 for infinite)
fnCallback function to invoke when timer expires
Returns
Unique timer identifier for later removal or reference
Exceptions
std::runtime_errorif no unique timer ID can be generated
See also
remove_timer()

Definition at line 52 of file loop.cpp.

◆ remove()

void zmqzext::loop_t::remove ( zmq::socket_ref  socket)

Unregister a socket from the event loop.

Removes a socket from event monitoring. The socket's handler callback will no longer be invoked.

Parameters
socketThe ZMQ socket to remove
Note
Removing a socket that was not registered is a no-op
It is safe to remove a socket within its own handler callback or from another callback
See also
add()

Definition at line 59 of file loop.cpp.

◆ remove_timer()

void zmqzext::loop_t::remove_timer ( timer_id_t  timer_id)

Unregister a timer from the event loop.

Removes a timer from the event loop. The timer's handler callback will no longer be invoked.

Parameters
timer_idThe unique identifier of the timer to remove
Note
Removing a timer that was not registered is a no-op
It is safe to remove a timer within its own handler callback or from another callback
See also
add_timer()

Definition at line 68 of file loop.cpp.

◆ run()

void zmqzext::loop_t::run ( bool  interruptible = true,
std::chrono::milliseconds  interruptCheckInterval = std::chrono::milliseconds{-1} 
)

Run the event loop.

Starts the event loop, which continuously monitors sockets and timers, invoking their respective callbacks when events occur. The loop blocks until terminated via signal interrupt, the termination of the context associated with any socket, callback return value is false, becomes empty (no sockets or timers registered anymore).

Parameters
interruptibleWhether to check for interrupt signals during loop execution (default is true) to finish the loop. When false, the loop will ignore any interrupt signals and will continue running (useful in actors orchestrated by a main application). See class documentation notes on interruptible behavior.
interruptCheckIntervalDuration between interrupt checks (when enabled) in milliseconds (default: -1 for checking only when interrupted by a signal, socket ready, or timer expiry). See class documentation notes on interrupt checking.
Note
This function blocks until the loop is terminated
See also
install_interrupt_handler()

Definition at line 77 of file loop.cpp.

◆ terminated()

bool zmqzext::loop_t::terminated ( ) const
inlinenoexcept

Check if the event loop has been terminated by interrupt signal or context termination.

Returns
true if the loop is in a terminated state, false otherwise
See also
run()

Definition at line 241 of file loop.h.


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