I’m a bit confused on how to load samples in a background thread. Just to be on the same page: The first few samples (in my case 4096) are preloaded when the app starts.
But what technique is now the best to load the rest? I tried my luck with a thread pool and it actually works pretty well. Even though the calls to addJob are possibly blocking I couldn’t notice a problematic performance impact.
But now I caught myself thinking a lock free queue would be a better solution. But how exactly would you implement the thread working the queue? A while loop and sleep 1ms if the queue is empty? Cached are only about 5ms so 1ms on Thread::wait could be a bit tuff (especially since 1ms can’t be guaranteed). Would Thread::yield be a better alternative, when the queue is empty?
How should the background thread load the samples? I read a lot about the memory mapped reader but I’m not quite sure how to use it correctly. Should the audio thread use the reader (using MMReader#read…) and the background thread just call MMReader#touchSample? If yes, in what range should the background thread touch the samples (1024, 2048 or am I completely off)?
As I wasn’t sure about the memory mapping, I ended up using only the normal reader. The ThreadPoolJobs read the samples into the audio buffer. (A little side note: I noticed a “weird” behaviour. Even though I perform a thread safe check if the background thread loaded the sample positions I’m accessing on the audio thread, the background thread kind of gets in the audio threads way when writing to the audio buffer while the audio thread is reading from it. The background thread is definitely NOT writing the same sample positions read by the audio thread. But some how the background thread manages to insert “NaN” into the audio buffer. Am I missing something? Shouldn’t I be able to read and write from/to a c-array aka. AudioBuffer concurrently when making sure I don’t do it at the same position?). Besides that, there are no problematic performance issues on this side either.
I’m not looking to the one correct answer that solves all my problems, just for some advice or experience concerning background threading the sample loading.