Best way to Work with Edits

I’m trying to figure out the best way to work with Edits if a couple of classes need access to the same Edit. Right now I’m working with a Singleton which holds a shared_ptr to the currently active Edit. But I feel thats not the ideal solution for threading issues and so on. So any tips ans tricks would be much appreciated.

Can you provide a bit more information about what you’re actually doing with the Edits? Is there a reason you need to access them from more than one thread concurrently?

The general idea of the Engine is that you call all the Edit methods on the message thread and it takes care of all the tricky real-time threading and audio device connection stuff behind the scenes.

Thank you very much for your insight. I use the Edit as some sort of project file. So there is always only one Edit active, and all classes that need to interact with it use the same one. So if I understand correctly, I should be fine if I ensure that all calls to the Edit are made from the message thread? Then my next question would be, how can I ensure/enforce that all calls are really on the message thread? I’m still a bit new to all of this and am glad about any kind of help :slightly_smiling_face:


There are assertions in the Edit methods to ensure calls are made from the message thread but you might want to add these to your own methods as well. You can use the JUCE_ASSERT_MESSAGE_THREAD macro.

However, I think this is looking at the problem slightly backwards. It would actually be harder to make calls from background threads since you have to spin them up. Usually, calls to model based stuff like Edit should be in response to events (mouse/keyboard presses) or listener callbacks. If you keep all of these on the message thread (all the JUCE UI callbacks are message-thread only) then you should be fine.

Also, run with Tsan on periodically to catch race conditions in your code.

1 Like

Alright, thanks so much!