Lock-free messaging?

Ok, so I know this comes up a lot, but I still don’t have a satisfying answer.
I need to be able to set a value from the audio thread, that after some dispatching gets written to a value tree on the message thread.

Just so I understand things correctly:

  1. AsyncUpdater::triggerAsyncUpdate() should not be called from the audio thread, because this will post a message, which allocates, i.e. blocks? This would have been my absolute favourite of doing what I’m trying to do.

  2. MessageManager::callAsync() allocates/blocks.

  3. Forum solution: Use a timer on the message thread that reads the value from a lock-free fifo or atomic.

I want to use this to build myself a threadsafe lock-free datamodel based on the value tree. Let’s say I want to use this with ~200 parameters. Each parameter would run a timer at 30 Hz to check for new value (so UI is able to update fast enough). I don’t like polling too much. Is there no better way to trigger an async update on the message thread? I really hate lock-free programing but I can’t run away anymore. :woozy_face:

I usually go to #3, using atomics and polling. Usually some sort of “needs changing” boolean (set by the audio thread/parameter changes) if the updating work is heavy and should only run if really needed

Wouldn’t this only need to be one timer that gets the values from all parameters?

Interesting… MessageManager::callAsync does indeed have a new AsyncCallInvoker<FunctionType> (functionToCall), and I suppose posting the message to the queue could ask the message array to allocate, but how risky is it really? IIRC that same method is used in the audio thread code inside of JUCE’s own Synthesiser / Graph implementation.

Wouldn’t this only need to be one timer that gets the values from all parameters?

Yes you are right. It’s more of a hypothetical. It feels like overhead to have the timers running. Come to think about it, it’s not even overhead. I have the feeling there’s something missing in my understanding of multithreaded programing and I’m willing to go one step further. So if anybody has some links or papers to things that could be used in this situation, please share.