4 ms·
If memory serves, Windows treats a sleep under the scheduler quantum length as a yield. It may take you off the cpu if there's something else to run but it may
by asveikau 2y ago
If memory serves, Windows treats a sleep under the scheduler quantum length as a yield. It may take you off the cpu if there's something else to run but it may not. Meanwhile burning up cycles may prevent low power states.
At any rate, back to the code at hand, there are many ways to block on SIGINT without polling. But it's also hugely odd that this code does not read events from the X11 socket while it does so. This is code smell, and a poorly behaved X client.
- orbisvicis 2y agoI thought that Linux behaved the same, but I'm not finding any proof in `man 2 nanosleep`...
- eqvinox 2y agoYou can't find that proof because Linux does the opposite. Unless your task is SCHED_REALTIME, all timers have a little bit of slack at the end that allows the kernel to group wakeup events. You can configure this (for non-RT tasks) with prctl(PR_SET_TIMERSLACK). https://lxr.linux.no/#linux+v6.7.1/kernel/time/hrtimer.c#L2094 https://lxr.linux.no/#linux+v6.7.1/kernel/time/hrtimer.c#L20... https://www.man7.org/linux/man-pages/man2/PR_SET_TIMERSLACK.2const.html https://www.man7.org/linux/man-pages/man2/PR_SET_TIMERSLACK....
- orbisvicis 2y agoSorry! I was looking for documentation that on Linux sleep(0) yields.
- eqvinox 2y agoThere is no code in nanosleep that converts it into a yield, and in fact a nanosleep(0) is a nanosleep(50µs) with the default timer slack value. If you want to yield, call sched_yield() …
- orbisvicis 2y agoI looked into this a bit further and it seems to be a range from [0,50]. [1] explains that if there is a pre-existing timer interrupt at 0, then the queue will be resumed at 0. But yes - given no other timers it will resume at 50. 1. https://people.kernel.org/joelfernandes/on-workings-of-hrtimers-slack-time-functionality https://people.kernel.org/joelfernandes/on-workings-of-hrtim...
- gpderetta 2y agoIt used to be the case that glibc implemented nanosleep for small values below the scheduling quantum with a spin loop. It was explicitly documented to do so. This was changed sometimes in the last 20 years, probably with battery powered devices becoming more prevalent and CPUs implementing more advanced sleep states.