Help with callFunctionOnMessageThread


#1

Hi all,

Apologies in advance if this is a bit of a noob question, but I'm not yet too proficient in the subtleties of C++, so function pointers are a new topic for me.

I'm trying to set up a callback from the callFunctionOnMessageThread method in order that I can process GUI events (hiding and showing a window) from a real-time thread.

What I have so far (super simplified), looks a bit like this:

class MyClass
{
private:

void* showMyWindow(void*);
void  ProcessStuffOnRealtimeThread();

ScopedPointer<DocumentWindow> myWindow;
}

void* MyClass::showMyWindow(void*)
{
myWindow = new DocumentWindow();
}

void MyClass::ProcessStuffOnRealtimeThread()
{
MessageManager* mm = MessageManager::getInstance();
mm->callFunctionOnMessageThread(MyClass::showMyWindow, nullptr);
}

Again, please don't laugh too hard at me :)

Thanks in advance,
Will

P.S. I read and re-read the documentation, plus all the material on function pointers that I could find and then just tried everything I could think of...


#2

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.


#3

Hi Jules,

Thanks so much for the quick response! The reason I was considering it was a) my real-time thread is actually handling controller messages, rather than audio, so it's not quite as critical and b) I'm not controlling the UI thread (I'm building a module that plugs into a 3rd party's application).

However, creating a timer callback is a much simpler solution anyway and will work fine for my needs, so I'll go with that. Thanks for the help!

Cheers,
Will