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

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


Op 2010-10-12 13:06, Michael Van Canneyt het geskryf:
> 
> Of course. It _IS_ wrong programming. Under windows you get away with it,
> because the messaging system bypasses the threads.

OK what do you propose is the "correct thread programming" solution to
tiOPF's "log to GUI" example I gave? I'm eager to learn. :) Just don't say:
Introduce multiple boolean FThread1Finished, FThread2Finished variables in
each of my applications, and surface all internal thread instances in the
various tiOPF classes to we could access their OnTerminate events? This
sounds all wrong to me.

Thanking about what you said, I can only guess the Windows developers (from
Delphi and even FPC) saw the frequent deadlock problem in TThread.WaitFor,
and decided to simply change its implementation from WaitForSingleObject()
to rather use MsgWaitForMultipleObjects() to work around the issue... and
more importantly, cater for what developers expected. Unfortunately such
work-arounds are not available in all FPC supported platforms, so currently
the "expected behaviour" and "actual behaviour" of TThread.WaitFor is very
different on other platforms. Having your application freeze is a rather
serious issue.

If your "correct programming with threads" was strictly adhered to, then
FPC developers should change the Windows implementation of TThread.WaitFor
to rather use WaitForSingleObject(). That way, at least there is
consistency across platforms. "single source, compiler everywhere" with
nice "consistent behaviour" and the all important "correct usage of thread
programming" is forced onto all. ;-)


> I will see about changing the specs.

I wouldn't hold by breath, that's why I simply opted to implemented a new
base class (TThread descendant) and implemented a new WaitFor() in there.
At least now it works as expected on all the platforms I use.

But unfortunately many other FPC developers will first have to experience
the problems, then have to jump through the same hoops I did, to get their
applications working.

Weren't you one of those that said a little while ago that TThread is
ultimately flawed anyway... so maybe a new implementation of WaitFor (and a
new definition of what it does) is a tiny step in the right direction.


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