[fpc-pascal] Where is IsMultiThreaded set under Linux?

Sven Barth pascaldragon at googlemail.com
Fri Oct 8 14:40:27 CEST 2010


Am 08.10.2010 14:03, schrieb Graeme Geldenhuys:
> Hi,
>
> On 8 October 2010 13:37, Sven Barth wrote:
>>
>> Note: You must start at least one thread (besides the main thread) to turn
>> "IsMultiThread" to true.
>
> I am, and changing my fpGUI code to the following does indeed prove
> that you are right. As soon as my thread is started, I see the '*****'
> output, but unfortunately only once. Let me explain.
>
>     if IsMultiThreaded then
>         begin
>           writeln('******');
>          CheckSynchronized();
>         end;
>
>
> What I am trying to debug is that as soon as I call mythread.waitfor,
> then WaitFor() blocks the applications main event loop, thus the
> application just hangs. This only occurs under X11, not under Windows.
>
> I can work around that problem, by doing the following.... I have to
> introduce a boolean variable in my thread class, set it to True at the
> end of the thread's Execute method, as follows:
>
> procedure TBarThread.Execute;
> begin
>    FFinished := False;    // work-around variable
>    while not Terminated do
>    begin
>      Synchronize(@UpdateProgressBar);
>    end;
>    FFinished := True;     // work-around variable
> end;
>
> ....Then use a while loop (instead of WaitFor), to check for that
> variable, and if not True, call fpgApplication.ProcessMessages
> instead.
>
> procedure TMainForm.ButtonClicked(Sender: TObject);
> begin
>    ProgressBar1.Position := ProgressBar1.Min;
>    if not Assigned(FThread) then
>    begin
>      writeln('program: creating thread...');
>      FThread := TBarThread.Create(True);
>      FThread.ProgressBar := ProgressBar1;
>    end;
>    writeln('program: starting the thread...');
>    FThread.Start;
>
>    writeln('program: waiting for thread...');
>    FThread.WaitFor;    // Can use this, it blocks the main event loop
>
>    // my work-around
>    while not FThread.Finished do
>    begin
>      sleep(100);
>      fpgApplication.ProcessMessages;
>    end;
>
>    writeln('program:  thread is finished!');
>    Label1.Text := 'Thread is done';
>    FreeAndNil(FThread);
> end;
>
>
>
> Obviously this is a hack, and damn ugly. I shouldn't need to do this.
> But still I haven't figured out why the application's main  event loop
> is blocked by WaitFor.
>
> Writing a console app, that does use a thread to count down from 20 to
> 0,  I can use a WaitFor, and it works (even under Linux). So clearly
> there is a problem in fpGUI, and more specifically in the X11 support
> code. Unbelievable that I haven't picked up on this sooner.... then
> again, I have never had a need for WaitFor usage until now.  :-/
>
>

1. If you do a WaitFor the calling thread will be suspended until the 
thread you called WaitFor on terminates (or the timeout elapses, but our 
WaitFor doesn't have a timeout parameter...). Thus it is normal that 
your main loop appears to hang.
2. Does your thread really terminate till then? Did you test a thread 
that does nothing and terminates immediatly?

I can't help you much with X11, cause I've not yet found the time and 
the need to dig into it.

Regards,
Sven



More information about the fpc-pascal mailing list