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

Michael Van Canneyt michael at freepascal.org
Wed Oct 13 09:39:11 CEST 2010



On Wed, 13 Oct 2010, Luca Olivetti wrote:

> En/na Michael Van Canneyt ha escrit:
>
>> 
>> and in "WaitFor" it says:
>> 
>> "Call WaitFor to obtain the value of ReturnValue when the
>> thread finishes executing. WaitFor doesn't return until the thread
>> terminates, so the thread must exit either by finishing the Execute method
>> or by exiting when the Terminated property is true."
>> 
>> There you have it. Nowhere does it say that if the main thread is blocked,
>> your synchronize() method will be executed nonetheless. Indeed, both specs
>> explicitly say that the calling thread is suspended or blocked (!) till the 
>> call returns.
>
>
> And in older versions of the documentation (delphi 2) it explicitly says
>
> "Don't call WaitFor in the context of the main VCL thread if your thread uses 
> Synchronize. Doing so will either cause a deadlock, making it appear that 
> your application has hung, or cause an EThread exception to be raised. 
> (Synchronize waits for the main VCL thread to enter the message loop before 
> allowing the method it is trying to synchronize to execute. If the main VCL 
> thread has called WaitFor, it won't enter the message loop and Synchronize 
> will never return. TThread detects that case and will raise an EThread 
> exception in the thread, causing it to terminate and, if not caught in the 
> Execute method, the application will terminate as well. If Synchronize is 
> already waiting on the main VCL thread when WaitFor is called, TThread can't 
> intervene, and your application will deadlock.)"
>
> They probably changed the implementation later on to avoid this, but old 
> farts like me still avoid to mix synchronize and waitfor.

... And Delphi 7 help explicitly mentions that Synchronize() does not work
in console applications (VCL and CLX), since it uses the message system.

Seems to me that there is plenty of cause for complaints :)

Michael.




More information about the Lazarus mailing list