MemoryMappedFile - at which time does the OS perform the file access?

I want to read some file in parts from within a realtime callback and consider doing this with a MemoryMappedFile. Side information: It is no typical audio file, so I need to roll my own reader class and cannot use any of the AudioFormatReaders coming with JUCE.

Now what’s not completely clear to me is when the actual file access is performed when the whole file is mapped. I guess that a particular section of the file be copied to memory as soon as this section is accessed through the pointer. So a straightforward implementation where this particular section is read from the real time thread as soon as the data is needed could lead to a file access which would interrupt the execution of the read access for an unpredictable time. Am I right here?

Assumed that I’m right, I think I get the idea behind the MemoryMappedAudioFormatReader::touchSample implementation. A solution for this problem could be “touching” some memory region some time before accessing it from the realtime thread on a background thread, but how long can I assume that this memory region stays in memory? So is there even any guarantee that it is still there until I access it from my realtime thread?

And furthermore, is there even a consistent answer that applies for all operating systems JUCE targets?

Would love to get some insight here, maybe from some of the DAW builders here that might already have done a bit research on this topic…

1 Like