zmq_timers.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. zmq_timers(3)
  2. ============
  3. NAME
  4. ----
  5. zmq_timers - helper functions for cross-platform timers callbacks
  6. SYNOPSIS
  7. --------
  8. *typedef void(zmq_timer_fn) (int 'timer_id', void *'arg');*
  9. *void *zmq_timers_new (void);*
  10. *int zmq_timers_destroy (void **'timers_p');*
  11. *int zmq_timers_add (void *'timers', size_t 'interval', zmq_timer_fn 'handler', void *'arg');*
  12. *int zmq_timers_cancel (void *'timers', int 'timer_id');*
  13. *int zmq_timers_set_interval (void *'timers', int 'timer_id', size_t 'interval');*
  14. *int zmq_timers_reset (void *'timers', int 'timer_id');*
  15. *long zmq_timers_timeout (void *'timers');*
  16. *int zmq_timers_execute (void *'timers');*
  17. DESCRIPTION
  18. -----------
  19. The _zmq_timers_*_ functions provide cross-platform access to timers callbacks.
  20. Once a timer has been registered, it will repeat at the specified interval until
  21. it gets manually cancelled. To run the callbacks, _zmq_timers_execute_ must be
  22. ran.
  23. _zmq_timers_new_ and _zmq_timers_destroy_ manage the lifetime of a timer
  24. instance. _zmq_timers_new_ creates and returns a new timer instance, while
  25. _zmq_timers_destroy_ destroys it. A pointer to a valid timer must be passed
  26. as the _timers_p_ argument of _zmq_timers_destroy_. In particular,
  27. _zmq_timers_destroy_ may not be called multiple times for the same timer
  28. instance. _zmq_timers_destroy_ sets the passed pointer to NULL in case of a
  29. successful execution.
  30. _zmq_timers_add_ and _zmq_timers_cancel_ manage the timers registered.
  31. _zmq_timers_add_ registers a new _timer_ with a given instance. _timers_ must
  32. point to a valid _timers_ object. The _interval_ parameter specifies the
  33. expiration of the timer in milliseconds. _handler_ and _arg_ specify the callback
  34. that will be invoked on expiration and the optional parameter that will be passed
  35. through. The callback must be a valid function implementing the _zmq_timer_fn_
  36. prototype. An ID will be returned that can be used to modify or cancel the timer.
  37. _zmq_timers_cancel_ will cancel the timer associated with _timer_id_ from the
  38. instance _timers_.
  39. _zmq_timers_set_interval_ will set the expiration of the timer associated with
  40. _timer_id_ from the instance _timers_ to _interval_ milliseconds into the future.
  41. _zmq_timers_reset_ will restart the timer associated with _timer_id_ from the
  42. instance _timers_.
  43. _zmq_timers_timeout_ will return the time left in milliseconds until the next
  44. timer registered with _timers_ expires.
  45. _zmq_timers_execute_ will run callbacks of all expired timers from the instance
  46. _timers_.
  47. THREAD SAFETY
  48. -------------
  49. Like most other 0MQ objects, timers are not thread-safe. All operations must
  50. be called from the same thread. Otherwise, behaviour is undefined.
  51. RETURN VALUE
  52. ------------
  53. _zmq_timers_new_ always returns a valid pointer to a poller.
  54. All functions that return an int, return -1 in case of a failure. In that case,
  55. zmq_errno() can be used to query the type of the error as described below.
  56. _zmq_timers_timeout_ returns the time left in milliseconds until the next
  57. timer registered with _timers_ expires, or -1 if there are no timers left.
  58. All other functions return 0 in case of a successful execution.
  59. ERRORS
  60. ------
  61. On _zmq_timers_destroy_, _zmq_poller_cancel_, _zmq_timers_set_interval_,
  62. _zmq_timers_reset_, zmq_timers_timeout_, and _zmq_timers_execute_:
  63. *EFAULT*::
  64. _timers_ did not point to a valid timer. Note that passing an
  65. invalid pointer (e.g. pointer to deallocated memory) may cause undefined
  66. behaviour (e.g. an access violation).
  67. On _zmq_timers_add_:
  68. *EFAULT*::
  69. _timers_ did not point to a valid timer or _handler_ did not point to a valid
  70. function.
  71. On _zmq_poller_cancel_, _zmq_timers_set_interval_ and zmq_timers_timeout_:
  72. *EINVAL*::
  73. _timer_id_ did not exist or was already cancelled.
  74. EXAMPLE
  75. -------
  76. .Add one timer with a simple callback that changes a boolean.
  77. ----
  78. void handler (int timer_id_, void *arg_)
  79. {
  80. (void) timer_id_; // Stop 'unused' compiler warnings
  81. *((bool *) arg_) = true;
  82. }
  83. ...
  84. void *timers = zmq_timers_new ();
  85. assert (timers);
  86. bool timer_invoked = false;
  87. const unsigned long full_timeout = 100;
  88. int timer_id =
  89. zmq_timers_add (timers, full_timeout, handler, &timer_invoked);
  90. assert (timer_id);
  91. // Timer should not have been invoked yet
  92. int rc = zmq_timers_execute (timers);
  93. assert (rc == 0);
  94. // Wait half the time and check again
  95. long timeout = zmq_timers_timeout (timers);
  96. assert (rc != -1);
  97. msleep (timeout / 2);
  98. rc = zmq_timers_execute (timers);
  99. assert (rc == 0);
  100. // Wait until the end
  101. rc = msleep (zmq_timers_timeout (timers));
  102. assert (rc == 0);
  103. assert (timer_invoked);
  104. rc = zmq_timers_destroy (&timers);
  105. assert (rc == 0);
  106. ----
  107. SEE ALSO
  108. --------
  109. linkzmq:zmq[7]
  110. AUTHORS
  111. -------
  112. This page was written by the 0MQ community. To make a change please
  113. read the 0MQ Contribution Policy at <http://www.zeromq.org/docs:contributing>.