For audio / video, as a rule, you do not need nano / micro seconds accuracy timers, milliseconds are enough. The most common packet spacing in audio is 20 ms. For this purpose, you need to use nanosleep.
In addition, if you are not using a real-time OS, you cannot guarantee that you get such good timings from the kernel.
Pitfall real-time sleep usage: they do not support the corresponding frequency (50 Hz for sound at 20 ms intervals). Because at each tick, you add processing time to the wait interval. Thus, the wait interval should be calculated based on the previous tick timestamp and the next expected timestamp.
You can implement a short intermediate timer (for example, 10 ms) with sleep modes and process events that should occur between these timer alarms when the timer is triggered. If you do this, you optimize the use of OS resources (minimizing process / thread context switching).
source share