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

**URL:** <https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106>\
**Category:** General JUCE discussion\
**Created:** [July 13, 2015, 9:34am UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106 "2015-07-13T09:34:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krollebolle](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Krollebolle](https://forum.juce.com/u/Krollebolle)\
**Post date:** [July 13, 2015, 9:34am UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106/1 "2015-07-13T09:34:50Z")

</div>

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(); };
juce::MessageManager::callAsync(myCoolLambda);
```

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.

---

<div class="post-metadata">

**Author:** ![jgerrard](https://avatars.discourse-cdn.com/v4/letter/j/b782af/32.png) [@jgerrard](https://forum.juce.com/u/jgerrard)\
**Post date:** [July 13, 2015, 10:17am UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106/2 "2015-07-13T10:17:00Z")

</div>

You can use callFunctionOnMessageThread for this&nbsp;(see&nbsp;http://www.juce.com/api/classMessageManager.html).

Hope this helps!

&nbsp;

---

<div class="post-metadata">

**Author:** ![Krollebolle](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Krollebolle](https://forum.juce.com/u/Krollebolle)\
**Post date:** [July 13, 2015, 11:22am UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106/3 "2015-07-13T11:22:00Z")

</div>

Yeah, I saw that function, but I also saw the following comment from jules here:&nbsp;http://www.juce.com/forum/topic/help-callfunctiononmessagethread

> ```
> 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&nbsp;callFunctionOnMessageThread &nbsp;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?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 13, 2015, 11:40am UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106/4 "2015-07-13T11:40:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Krollebolle](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Krollebolle](https://forum.juce.com/u/Krollebolle)\
**Post date:** [July 13, 2015, 2:07pm UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106/5 "2015-07-13T14:07:06Z")

</div>

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

> Always create a new instance of a CallbackMessage on the heap, as it will be&nbsp;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
    message->post();
}
```

where myCallbackMessage is nothing but

**MyCallbackMessage.h**

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

private:
    juce::OptionalScopedPointer<QAHandler> mQAHandler;
};
```

**MyCallbackMessage.cpp**

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

GetAndRunQAJsonOnJuceMessageThread::~GetAndRunQAJsonOnJuceMessageThread()
{
}

void GetAndRunQAJsonOnJuceMessageThread::messageCallback()
{
    mQAHandler->getAndRunQAJson();
}
```

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [July 13, 2015, 3:20pm UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106/6 "2015-07-13T15:20:07Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Krollebolle](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Krollebolle](https://forum.juce.com/u/Krollebolle)\
**Post date:** [July 14, 2015, 6:19am UTC](https://forum.juce.com/t/call-back-to-the-juce-message-thread-in-a-non-c-11-way/15106/7 "2015-07-14T06:19:35Z")

</div>

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