Call back to the Juce Message Thread in a non-C++11 way


I am using Juce together with an internal SDK which has to be non-C++11 to support some old and exotic devices. Up until now I have been using juce::MessageManager::callAsync to call back to the Juce Message Thread from OpenGL Rendering threads when needed. This is, however, done using std::function/lambdas which are C++11.

So how can I call back to the Juce Message Thread in a non-C++11 way?

As an example, this is how I currently do it:

std::function<void(void)> myCoolLambda = [this]() { this->myCoolMemberFunctiion(); };

I could not find a way to jump into the Juce Message Thread in the JuceApplication class. If that's possible, I could set a state variable from the OpenGL Thread and let the Juce Message Thread handle it when applicable.


You can use callFunctionOnMessageThread for this (see

Hope this helps!



Yeah, I saw that function, but I also saw the following comment from jules here:

Agh! No no no no...

You probably never want to call callFunctionOnMessageThread() - it has serious potential to deadlock. But even if it didn’t, calling it on your real-time thread is probably the worst place you could possibly use it! It blocks until the function has returned, so it’ll block your audio thread. (And possibly deadlock it too)

Just get yourself a shared atomic variable, write it in your audio thread, and use a timer or something in your UI thread to check it for changes.

This made me wonder whether it is a good idea to use callFunctionOnMessageThread  or not. And I have to call it from a real-time thread (the OpenGL Rendering thread) and jules points out that this is a bad place to call it. Or is it not?


Yes, it's a bad idea to call it! Because it blocks the message thread.

The best non-C++11 way to invoke something asynchronously is with a CallbackMessage, which is safe because it's non-blocking.


Thanks, CallbackMessage was easy to use and seems to work fine. Just to confirm: The description of the CallbackMessage states

Always create a new instance of a CallbackMessage on the heap, as it will be deleted automatically after the message has been delivered.

Does this mean that the following is ok to do?

    MyCallbackMessage* message = new MyCallbackMessage(this); // this is a QAHandler object

where myCallbackMessage is nothing but


class MyCallbackMessage : public juce::CallbackMessage
    MyCallbackMessage(QAHandler* const qaHandler);
    void messageCallback() override;

    juce::OptionalScopedPointer<QAHandler> mQAHandler;


MyCallbackMessage::MyCallbackMessage(QAHandler* const qaHandler) : mQAHandler(juce::OptionalScopedPointer<QAHandler>(qaHandler, false))


void GetAndRunQAJsonOnJuceMessageThread::messageCallback()


AFAIK even the following code is possible (with Foo as a CallbackMessage).

(new Foo())->post();


Ok, cool, I'll do something of this sort then.