Thread-safe Messaging

I seem to remember something being thread-safe for messaging, but I’m having trouble getting a explicit answer from the docs or forum search.

Are messages juce’s best thread-safe queue mechanism? If I post a message, I can pick it up in another thread with no problem? Or do I need to use any locks?

Then I suppose my other question would be about cross-thread memory allocation. The last stuff I was doing used Apple’s MP API. Any memory allocated in a thread had to be created with their API, and freed the same way, obviously - now that was OS 9. In OS X/Linux and modern Win (say XP and after) do I have those limitations? Can I, for instance, create an object in one thread, then hand it off via pointer to another to be freed there?



i don’t know about the juce coverage here but i do know that you can new on one thread and delete from any other thread on any of the win32 OSes. i think that was one of your questions.

{warning: potentially meaningless deviation}
one gotcha, at least one of the lesser known ones on win32 i guess, is that if you ever forcibly terminate a thread, say it’s hung, with the killThread or terminateThread() api (forget the actual name) then you leak that thread’s stack. and as far as my research revealed there was nothing you could do to get that mem deallocated. of course, that’s only if you are killing the thread forcibly per se; if you exit it gracefully this isn’t a problem and the stack goes away with the thread.

anyway, as long as the object you’re allocating is on the heap you can delete from any thread on win32.

There’s no need to lock to post a message, but of course it’ll only be delivered to the message thread.

Oh well, that’s not what I’m looking for then. Is there any built-in mechanism for thread to thread communications? Do any of the action or listener classes use a thread-safe mechanism?

Or should I roll my own? I don’t see a queue system built-in, but of course locks and juce containers should work.


Most of the action and listener classes are thread-safe but again, they just receive messages on the message thread. If you need to communicate between two of your own threads, waitableevents and locks are the way to go.