How to use multi-core in processBlock function


#1

Hi,

I'd like to optimize my processBlock function using some parallel processing in order to improve performances on arm multi-core target.

My software generates two sounds with some effects that are mixed together. The current processing chain is the following:

MIDI Note processing => Sound 1 generation => Sound 2 generation => Sound 1 & 2 mix

I'd like to have something like:

                                     => Sound 1 Generation =>

MIDI Note processing -|                                         |= Sound 1 & 2 mix

                                     => Sound 2 Generation =>

 

I tried to put the Sound 2 Generation into a thread which run() method looks like this

   while (threadShouldExit()==false)
    {

      workInProgress = true;

      // sound 2 generation

     workInProgress = false;

     wait(-1);

   }

 

In the ProcessBlock function the thread is called like this :

//MIDI Note processing

sound_2_thread->notify()  // start the Sound 2 generation on the thread

// Sound 1 generation

while (sound_2_thread->workInProgress=true); // wait for Sound 2 generation to finish if necessary

// Sound 1 & 2 mix

 

Well, this works on OSX in debug mode but not at all in release mode or on ARM target. The ProgressBlock got stuck into the

while (sound_2_thread->workInProgress=true);

 

I know the ProcessBlock function is very sensitive so what would be a good way to achieve parallel processing in the ProcessBlock ?

 

Thanks for your help !

 


#2

Someone else asked the same thing recently:

http://www.juce.com/forum/topic/wait-all-jobs-finish-threadpool

The TL;DR is that

a) It's surprisingly hard to make it work reliably

b) In practice, for almost all plugins it's going to make things worse rather than better

..but in your case, if you only have one other thread, then it could possibly be justifiable. Just make sure you don't use any locks, and you'll probably want to use a spin-lock for your audio thread to wait for the other one.