JUCE Threads, strange deadlock

Hi all,
I am experiencing a weird problem when updating UI from a thread.

Threads are as follows:

Thread1 (main thread):

  1. has locked message manger
  2. control is currently in a button clicked
  3. this thread needs to stop Thread2 which is updating UI.(cannot continue untill the thread2 exits/stops)

Therad2(worker thread)

  1. Doing some work…
  2. Tries to lock the message-manager since it needs to update the UI.
    But Thread 1 (main thread) has already locked the message manager.

    Deadlock:

    Thread1 has locked the messagemanger and is waiting for thread2 to exit.
    Thread2 in its run method trying to Lock the messagemanger( already locked by Thread1)

This could be bad programming practice/strategy from my side rather than
juce issue. I am just asking for ideas or work around.

Thanks in advance for any ideas or help

Yogesh

Yes, I’ve been in that situation before and it’s just a good old-fashioned deadlock. There’s no easy solution except:

  • don’t wait in the UI thread for the other one to stop - instead call its signalThreadShouldExit method and have the thread itself call your clean-up code just before it exits its run method.

  • never lock the MM in the other thread, but instead when it wants to do some UI stuff, make it trigger events that get processed in the UI thread. This is what I normally do, as it’s very safe.

Just thinking about it, though, I wonder if it’d be possible to write a special kind of MM lock that can fail if the thread gets told to exit… It’d be annoying to use, but could at least handle these cases. Hmm…

ok, have a look at the tip in SVN… I’ve checked in a new version with a tweak to MessageManagerLock - see if it helps…

Cool Idea,
Seems to be working for me. Deadlock gone
Thanks a Lot :smiley:


Yogesh

Ok, this is exactly the #### I encounter