showMessageBoxAsync and locks


#1

The message thread waits for a CriticalSection on a database. I have a second thread which is holding a lock on the database. The second thread wants to fail, hits an error, and calls showMessageBoxAsync which then gets into a deadlock.

Options:

  • Don’t hold a lock when trying to display an error. This is a bit awkward - probably easy if I was using exceptions but requires a lot of rejigging stuff.
  • Write a version of showMessageBoxAsync which is fire and forget and returns immediately eventually showing a message box.

But I don’t understand why showMessageBoxAsync is waiting for the message to be processed by the message thread before returning - seems the opposite of asynchronous…? It sits on callFunctionOnMessageThread … message->finished.wait() - this looks unnecessary?


#2

It only does that if you try to launch the message box from a background thread - in that case it has to wait for the message thread to actually set up the box - I don’t think there’s any alternative to that, because the UI stuff must happen on the message thread.


#3

As there’s no return value to care about, I don’t think, what does it have to block for?


#4

Hmm. Well, I guess it could return before the box has actually been created, but that feels somehow dodgy to me…

Though I guess opening an alert window from another thread is inherently racy anyway, so it probably wouldn’t be any more dangerous than the way it works now…


#5

I’m not sure if it’s dodgy.

I’d have throught that the Async version would launch a message box sometime later … but the locking adds synchronisation?


#6

Yeah, it ensures that the box exists and is the modal component before returning. The parameters do include a component pointer, which is why it makes me nervous to just use that at some point in the future…


#7

Okay - well - it I suppose the documentation could just elaborate on what’s going on:

Currently it says:

Shows a dialog box that just has a message and a single button to get rid of it.

The box will be displayed and placed into a modal state, but this method will return immediately, and if a callback was supplied, it will be invoked later when the user dismisses the box.

To…

Shows a modal dialog box that just has a message and a single button to get rid of it.

However, unlike the non-asynchronous version (showMessageBox), showMessageBoxAsync will return before the user dismisses the window.

If a callback was supplied, it will be invoked later when the user dismisses the box.

Thead-safety: This method is safe to call from any thread. However it will block until the message thread has displayed the dialog box. Therefore when calling the method do not hold any locks that may also be held by the message thread or you may get a deadlock.

The callback will take place on the message thread.