----- Forwarded message from Fons Adriaensen <fons(a)linuxaudio.org> -----
Date: Thu, 19 May 2022 22:22:12 +0200
From: Fons Adriaensen <fons(a)linuxaudio.org>
To: "Jeanette C." <julien(a)mail.upb.de>
On Thu, May 19, 2022 at 07:52:33PM +0200, Jeanette C. wrote:
I know about one or two applications that use the
timeofday/sleep mechanism,
but from first hand experience I know that these tend to drift and wobble.
The key to do this is to have a high priority thread waiting for an
*absolute* time, and then each time increment that time by the
required delta.
Note that this is fundamentally different from using sleep or similar
functions. With those you wait for a certain time. So if your previous
event was late, the next one will be late as well just because you
start waiting for it too late. So all the errors will add up, and
you will *never* get the correct event frequency.
When you wait until an absolute time, any latency on the previous
event does not affect the following ones. The errors don't accumulate.
So what to wait for ? That could be any system call that takes an
absolute timeout rather than a maximum waiting time. On Linux I'd
use something like sem_timedwait(). To set the initial timeout,
the corresponding clock can be read with clock_gettime(), using the
CLOCK_MONOTONIC option.
Don't know about Apple. Last time I looked it didn't have clock_gettime(),
but it has gettimeofday(). Note that it is not gettimeofday() that is
the cause of the problem you mentioned, it is using sleep() or usleep().
Ciao,
--
FA
----- End forwarded message -----