Usually I was doing thread syncs (UI->audio and audio->UI) using atomic flags, shared_ptr and fifos assuming only one of the threads write data. There were no problems with that.
However, how do you sync in a lockfree way some complex data (think in a sequence of notes and events where you can add or remove elements) when both thread can read or write on them? I thought different solutions, but I’m not satisfied with them:
Using two FIFOs to notify changes between cloned datas. However this may be problematic. Assuming I read one FIFO at the beginning of the processblock and the other one in a timer, the audio thread could write a “delete blob Z” (in the fifo and its own audio data) while the message thread could write a “modify blob Z” (in its fifo and its own ui data)… depending which one comes first it I could have different results, even some de-synchronization or bug. (I could use weak_ptr here to check).
Using a FIFO for audio->UI, and a dirty flag for ui->audio. The UI is reading the fifo with a 40ms timer, and it prepares a copy of the full data for the audio thread. The audio thread checks at the begging if there are new data to deprecate the old one. The problem with this approach is that the changes you do in the audio thread to the data will have a delay! This is a problem for noteOn which won’t be played.
I can think in other solutions, but all of them seem tedious and prone to have bugs. Is there a better method?