[Lazarus] TThread.WaitFor blocks the main event loop under Linux

Graeme Geldenhuys graemeg.lists at gmail.com
Tue Oct 12 12:14:53 CEST 2010


Op 2010-10-12 11:43, Michael Van Canneyt het geskryf:
> 
> I am all for it, but first explain the following:
> 
> WaitFor by itself cannot cause deadlock.
> 
> There must be another call as well, I assume Synchronize(), in the thread
> that you are waiting on. Is that correct  ?

Correct.


> Because, if it is so, then you are simply programming wrong. Your coding
> style causes deadlock.
> 
> Thread 1 (main thread) says: I'll wait for thread 2;
> Thread 2 says: I'll wait for thread 1 (implicitly, in sycnhronize)

Please take a look at tiOPF's tiLog and tiThread units to see the usage.
Taking the logging units as an example. So loggers need synchronize (eg;
when writing to a GUI log window), other loggers don't need synchronize
(eg; when outputing to the console).

Threading 101: If you access the GUI from a thread, you need to use
synchronize.

Now my application starts up with a GUI log window (tiOPF normally uses -lv
parameter for that). I instantly created 1 million log events. The GUI
logger is cached to not slow down the GUI (main thread), so it takes a
while before all those 1 million log events appear in the log window. I
then decide to quit the application. The GUI logging thread is still busy
processing the 1 million log events, so the main thread (application) has
to wait for the GUI logger to finish, because it terminates the
application. The thread uses synchronize because it it's writing to a GUI
window. There is your DEADLOCK under Linux! My application doesn't need to
keep track of such logger thread, the tiOPF frame handles that all for me,
by creating and destroying those logger classes when needed. So I have no
OnTerminate event handlers or Boolean IsLoggerFinished in my application.

This is just one example of where tiOPF uses threads. There is pooled
threads, logger threads, thread list, app server thread etc... Simply
search the tiOPF source for 'WaitFor' and you'll find lots of different
examples. I can't think all of them is just wrong??

> So what you propose is a change in specs, namely: WaitFor() is blocking
> *except* in the main thread, where synchronize calls are still executed.
> Main reason for this change is to avoid potential deadlocks.
> 
> Is this correct ?

Yes, that sounds about right.



Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net:8080/fpgui/






More information about the Lazarus mailing list