ZMQ_TIMERS(3)
π NAME π
zmq_timers β helper functions for cross-platform timers callbacks β¨
Like, this is the page from the 0MQ Manual, and it's about timers that go ding on every platform, no cap π
πΈ SYNOPSIS πΈ
typedef void(zmq_timer_fn) (int timer_id, void *arg);
void *zmq_timers_new (void);
int zmq_timers_destroy (void *timers_p);*
int zmq_timers_add (void *timers, size_t interval, zmq_timer_fn handler,
void *arg);
int zmq_timers_cancel (void *timers, int timer_id);
int zmq_timers_set_interval (void *timers, int timer_id, size_t
interval);
int zmq_timers_reset (void *timers, int timer_id);
long zmq_timers_timeout (void *timers);
int zmq_timers_execute (void *timers);
β¨ DESCRIPTION β¨
okay so the zmq_timers* functions give you cross-platform access to timers callbacks. Once a timer has been registered, bestie, it will repeat at the specified interval until it gets manually cancelled β it's giving main character who never leaves the group chat π. And to actually run the callbacks, you gotta run zmq_timers_execute.
zmq_timers_newandzmq_timers_destroyβ these two manage the lifetime of a timer instance π«Άzmq_timers_newcreates and returns a new timer instance, whilezmq_timers_destroydestroys it. A pointer to a valid timer must be passed as thetimers_pargument ofzmq_timers_destroy. And like β in particular,zmq_timers_destroymay not be called multiple times for the same timer instance π«. On a successful execution,zmq_timers_destroysets the passed pointer toNULLβzmq_timers_addandzmq_timers_cancelβ these two manage the timers registered π¦zmq_timers_addβ registers a new timer with a given instance.timersmust point to a valid timers object, fr fr. Theintervalparameter specifies the expiration of the timer in milliseconds.handlerandargspecify the callback that will be invoked on expiration and the optional parameter that will be passed through π« The callback must be a valid function implementing thezmq_timer_fnprototype. An ID will be returned that can be used to modify or cancel the timer πzmq_timers_cancelβ will cancel the timer associated withtimer_idfrom the instancetimersβοΈzmq_timers_set_intervalβ will set the expiration of the timer associated withtimer_idfrom the instancetimerstointervalmilliseconds into the future β³zmq_timers_resetβ will restart the timer associated withtimer_idfrom the instancetimersπzmq_timers_timeoutβ will return the time left in milliseconds until the next timer registered withtimersexpires β°zmq_timers_executeβ will run callbacks of all expired timers from the instancetimersπβοΈ
π§΅ THREAD SAFETY π§΅
Like most other 0MQ objects, timers are not thread-safe π All operations must be called from the same thread. Otherwise, behaviour is undefined β and not in a cute way, bestie π
π RETURN VALUE π
zmq_timers_newalways returns a valid pointer to a poller β¨- All functions that return an
intreturn-1in case of a failure. In that case,zmq_errno()can be used to query the type of the error as described below π zmq_timers_timeoutreturns the time left in milliseconds until the next timer registered withtimersexpires, or-1if there are no timers left π°οΈ- All other functions return
0in case of a successful execution π«Ά
π ERRORS π
On zmq_timers_destroy, zmq_poller_cancel, zmq_timers_set_interval, zmq_timers_reset, zmq_timers_timeout_, and zmq_timers_execute:
EFAULTβtimersdid not point to a valid timer π¬ Note that passing an invalid pointer (e.g. pointer to deallocated memory) may cause undefined behaviour (e.g. an access violation). yeah. don't do that.
On zmq_timers_add:
EFAULTβtimersdid not point to a valid timer orhandlerdid not point to a valid function π
On zmq_poller_cancel, zmq_timers_set_interval and zmq_timers_timeout_:
EINVALβtimer_iddid not exist or was already cancelled π«
π EXAMPLE π
Add one timer with a simple callback that changes a boolean.. π₯ (yes, with the double dot, we're keeping it, it's iconic)
void handler (int timer_id_, void *arg_)
{
(void) timer_id_; // Stop 'unused' compiler warnings
*((bool *) arg_) = true;
}
...
void *timers = zmq_timers_new ();
assert (timers);
bool timer_invoked = false;
const unsigned long full_timeout = 100;
int timer_id =
zmq_timers_add (timers, full_timeout, handler, &timer_invoked);
assert (timer_id);
// Timer should not have been invoked yet
int rc = zmq_timers_execute (timers);
assert (rc == 0);
// Wait half the time and check again
long timeout = zmq_timers_timeout (timers);
assert (rc != -1);
msleep (timeout / 2);
rc = zmq_timers_execute (timers);
assert (rc == 0);
// Wait until the end
rc = msleep (zmq_timers_timeout (timers));
assert (rc == 0);
assert (timer_invoked);
rc = zmq_timers_destroy (&timers);
assert (rc == 0);
π SEE ALSO π
π AUTHORS π
This page was written by the 0MQ community π«Ά To make a change please read the 0MQ Contribution Policy at http://www.zeromq.org/docs:contributing.
okay that's the whole thing bestie, you're so welcome π this was 0MQ 4.3.5, dated 10/09/2023, and honestly? iconic. periodt. π
π¦ where this page comes from
- epel 10: zeromq 4.3.5-18.el10_0 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 43: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 43: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 44: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- fedora 44: zeromq 4.3.5-22.fc43 Β· MPL-2.0 AND BSD-3-Clause AND MIT
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- opensuse-leap 16.0: zeromq-devel 4.3.5-160000.2.2 Β· MPL-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms