Separate thread affecting the processing thread when allocating

Hi,

I have been debugging a strange phenomenon in Windows 10. When I allocate large pieces of memory (100’s of Mb in several AudioBuffer’s) from a separate thread (in normal priority), I can hear glitches in the audio meaning that the processing thread is disrupted. This task is launched from the message thread and - to make sure nothing else is wrong - I have replaced the allocation for 3 seconds loop and the processing wasn’t affected in that case.

Anybody can explain why this is happening? I’m puzzled.

Maybe you have some kind of unexpected heap allocation already happening in the audio thread and that causes some kind of locking problem when you do the allocation in the other thread?

Also, I am assuming the buffers you allocate somehow have to be given/switched to be used by the audio thread once they have been allocated? How are you handling that?

The problem with heap allocation is that you have no idea how long its going to take. It needs to be threadsafe across diffrrent priority threads, so it’s susceptible to priority inversion.

Generally, heap allocation or deallocation on the audio thread is a very bad idea.