event loop and timers
Peter Harris
peter at harr.ca
Sun Sep 1 18:56:19 UTC 2024
On 2024-09-01 9:30 a.m., Steven J Abner wrote:
> Having an issue with using timers with 'beginner loop'.
> Any insight would be greatly appreciated!
Events are difficult to get right, and your proposed solution has a
battery-eating busy-loop. The general way to handle timers is to use
xcb_get_file_descriptor() with select/poll/epoll.
Something like (pseudocode):
while (running) {
while (ev = xcb_poll_for_event(c)) {
process_event(ev);
}
// process_timers runs any timers that are immediately pending,
// and returns the number of milliseconds until the next timer
// in the queue is ready.
// process_timers returns INT_MAX if there are no timers.
timeout = process_timers();
poll(xcb_get_file_descriptor(c), timeout);
}
Note that if process_timers() calls (even indirectly) any xcb_*_reply
functions, you'll need a xcb_poll_for_queued_event() loop before calling
poll().
Peter Harris
More information about the Xcb
mailing list