I have the following situation:
I have a straightforward SampleReader object that plays back an audio snippet by copying frames of audio from a pre-loaded AudioBuffer (containing the audio data) into the host’s
bufferToFill on the audio thread.
At the same time, I have a SampleLoader object that might, at any time, load new data into said AudioBuffer on another thread, or delete that data.
Of course, I need to synchronise the two such that SampleLoader doesn’t overwrite/delete the audio data while the SampleReader is reading it.
If the SampleLoader is currently manipulating the data, the SampleReader should simply return from the audio callback and do nothing.
Obviously, the SampleReader is not allowed to do any locking or other non-realtime-safe operation.
What is the recommended JUCE way to do this these days?
std::mutex::lock()in the SampleLoader and
std::mutex::try_lock() in the SampleReader?
juce::SpinLock::enter()in the SampleLoader and
juce::SpinLock::tryEnter()in the SampleReader?
- something else?
I might have one hundred of those SampleReaders going on simultaneously, so whatever try-lock-ish operation they do in their audio callback, it will be called a lot.