petrinm/SoapyShared

Synchronization between seeder and leecher

tejeez opened this issue · 1 comments

Using sleep to check for new data in the shared buffer can cause problems for applications requiring low latency, especially if the delay is not configurable. As suggested in tejeez/shmbuffer#1 , a process-shared condition variable could be used instead. I think it could work like this:

After seeder has copied a buffer of samples from slave to shared memory, it calls pthread_cond_broadcast. When leecher finds there is not enough new data in the shared buffer, it calls pthread_cond_wait and then checks again. To implement timeout, it could call pthread_cond_timedwait instead.

A broadcast condition variable is ignored by processes not waiting for it, so it should work nicely without extra overhead even if an application processes data in larger blocks and misses some notifications in between.

The frequency of notifications would be determined by the buffer size used in the seeder. This means the minimum latency could be controlled by configuring this buffer size, which makes sense since it determines the minimum possible latency anyway.

If POSIX shared memory is replaced by boost for better cross-platform support, a similar mechanism seems to be boost::interprocess::interprocess_condition functions notify_all and timed_wait.

Migration to Boost for cross-platform support started in the boost-branch.
https://github.com/petrinm/SoapyShared/tree/boost