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

fluisgirardi at gmail.com fluisgirardi at gmail.com
Fri Oct 8 15:43:09 CEST 2010


I forget: It works only with FPC 2.4, because TEvent.WaitFor works correctly
with timeouts.


Fabio

2010/10/8 fluisgirardi at gmail.com <fluisgirardi at gmail.com>

> Hi!
>
> I got the same when doing some classes for my project. My solution is add a
> TEvent in my thread that will be signaled in the last line of the thread
> loop.
>
> On my application instead use TThread.WaitFor, I use TThread.MyWaitFor that
> internals do:
>
> While TEvent.WaitFor(1)<>wrSignaled do
>    Application.ProcessMessages;
>
> It solves my problem with threads finalization on windows and linux.
>
>
> Fabio
>
> 2010/10/8 Graeme Geldenhuys <graemeg.lists at gmail.com>
>
>> Hi,
>>
>> [Just as well the Lazarus IDE didn't get the thread update mentioned a
>> few days ago... where a thread was doing work, and the main thread was
>> waiting for that thread to finish... it wouldn't have worked under
>> Linux it seems.]
>>
>>
>> I just stumbled across this issue in fpGUI, and I see MSEgui and
>> LCL-GTK2 has the
>> exact same problem. See attached project. I haven't tried LCL-QT, but I
>> presume
>> that same issue will persist.
>>
>> A simple program (just for illustrative purposes), where a thread
>> updates the progress bar (the thread could be doing any long running
>> process).
>> Click the button, it creates a thread and
>> starts it off. If then waits for the thread to finish, then displays
>> that it has finished in the GUI, and free's the thread manually.
>>
>> If you use the MyThread.WaitFor to wait for the thread to finish, then
>> the application is instantly frozen (even the thread), nothing gets
>> updated and you have to kill the process. For some reason
>> WaitFor blocks the main thread's event loop. It seems this issue is
>> only under FPC apps and under X11 (no matter the GUI toolkit),
>> it works fine under Windows.
>>
>> The work around, is to use a boolean variable in the thread class, and
>> a while loop which checks if the boolean variable is true, if not it
>> calls Application.ProcessMessages (to keep the app alive).
>>
>> Like I said, this is a stupid example, but it illustrates that WaitFor
>> cannot be used under X11 (for our GUI toolkits). Any ideas as to why
>> WaitFor blocks the main event loop? Is it a Linux thing, X11 thing, or
>> just that we made a mistake somewhere?
>>
>> So what is wrong? Is it the TThread.WaitFor implementation that is
>> flawed? Is it X11 that is flawed, or just GUI toolkit developers that
>> don't know how to code (thought I doubt it's the latter). :)
>>
>> As an experiment (I'm all out of ideas now), I dived into the
>> TThread.WaitFor implemenation, as saw that it calls
>> WaitForThreadTerminate() with no timeout value. I though, well, let me
>> try calling WaitForThreadTerminate() directly with say a 2 second
>> timeout. No luck there. The instant you call that, the application is
>> frozen. It never times out either, so the FPC+Unix
>> WaitForThreadTerminate() functions seems broken too. :-/
>>
>> I guess I need to file a FPC bug report about WaitForThreadTerminate()
>> being broken under Linux, but I wanted others to confirm this first,
>> before I do.
>>
>> Did anybody know about this? Anybody know how we can get
>> TThread.WaitFor to actually work under Linux/Unix?
>>
>>
>> --
>> Regards,
>>  - Graeme -
>>
>>
>> _______________________________________________
>> fpGUI - a cross-platform Free Pascal GUI toolkit
>> http://opensoft.homeip.net:8080/fpgui/
>>
>> --
>> _______________________________________________
>> Lazarus mailing list
>> Lazarus at lists.lazarus.freepascal.org
>> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20101008/60ff438b/attachment-0004.html>


More information about the Lazarus mailing list