POSIX Timers: Asynchronous Notification#
Basics#
POSIX timers:
timer_create()(andtimer_delete(), unshown in examples)delivery/expiry: (async) signal, thread (-> later)
choose signal to use (never queued, not even rt signals)
setup handler for it (see Asynchronous Signal Handling)
timer_create()to send that signal on expiryevent.sigev_notify = SIGEV_SIGNAL; // <-- alternatives? event.sigev_signo = SIGRTMIN;
One-shot Timers#
one shot (
code/oneshot.cpp)it_intervalis 0 -> oneshotexpiry at
it_valuenothing new regarding signals
Periodic Timers#
periodic (
code/periodic.cpp)note
it_intervalnot 0wrap in infinite loop
sigaction(SA_SIGINFO)#
link to sync handling/
sigwaitinfo()multiplexing multiple timers on one signal via siginfo_t
jjj link from signals/async
only 32 rt signals
might want to use more timers
continue from posix-periodic.cpp
add 2nd timer (and arm it differently)
remove “interrupted” in main loop
-> handler cannot say which
solution
step 1: sigaction -> SA_SIGINFO, sa_sigaction (more info in handler)
handler signature: void(int sig, siginfo_t* info, void*)
in handler, use
info->si_valuemake timer1, timer2 visible in signal handler (-> global)
event notification setup
event.sival_ptr = &timer{1,2} (-> handler’s info->si_ptr)