Hi all,
I am experiencing a weird problem when updating UI from a thread.
Threads are as follows:
Thread1 (main thread):
has locked message manger
control is currently in a button clicked
this thread needs to stop Thread2 which is updating UI.(cannot continue untill the thread2 exits/stops)
Therad2(worker thread)
Doing some work…
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.
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…