Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2016-07

TThread in console mode program.

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Donald Ziesig

Hi All:

I am still trying to get timers to work in a console mode program. While I was doing that I ran into problems with TThread.

Question: Do console mode programs have a "run loop"?. The reason I am asking is that both Raspbian and Linux Mint TThread's Synchronize method apparently does nothing (it works fine when used with LCL). I can call it, but it doesn't run the method passed as a parameter, nor does it return.

I wrote a trivial console mode program that uses TThread and tested it on both Raspbian and Linux Mint with exactly the same results.

The simplest Execute method fails:

procedure TXThread.Execute;
begin
   Synchronize( Callback );
end;

I have tried stepping into Synchronize, but even the debugger seems to get lost.

If I change Execute to:

procedure TXThread.Execute;
begin
   Callback;
end;

the Callback method is invoked (seemingly in the main thread, but I'm not sure... Will have to figure it out later).

Thanks,

Don Ziesig

Reply by email

Donald Ziesig
to Mattias Gaertner
Mattias Gaertner wrote 11 lines
On Thu, 21 Jul 2016 15:05:17 -0400 Donald Ziesig wrote: > [...] > Question: Do console mode programs have a "run loop"?. Synchronize requires that your main thread calls CheckSynchronize from time to time. The LCL does that in its loop. > [...] Mattias

That made it work! Thank you. (Where is this documented?)

Don

Reply by email

Mattias Gaertner
to Donald Ziesig
Show quoted text 14 lines
On 07/21/2016 03:21 PM, Mattias Gaertner wrote: > On Thu, 21 Jul 2016 15:05:17 -0400 > Donald Ziesig wrote: > >> [...] >> Question: Do console mode programs have a "run loop"?. > Synchronize requires that your main thread calls CheckSynchronize from > time to time. The LCL does that in its loop. > > >> [...] > Mattias That made it work! Thank you. (Where is this documented?)

In CheckSynchronize itself, but not in the wiki multithread tutorial. I added it there.

Mattias

Reply by email

Donald Ziesig
to Mattias Gaertner
Mattias Gaertner wrote 20 lines
On Thu, 21 Jul 2016 15:35:51 -0400 Donald Ziesig wrote: > On 07/21/2016 03:21 PM, Mattias Gaertner wrote: >> On Thu, 21 Jul 2016 15:05:17 -0400 >> Donald Ziesig wrote: >> >>> [...] >>> Question: Do console mode programs have a "run loop"?. >> Synchronize requires that your main thread calls CheckSynchronize from >> time to time. The LCL does that in its loop. >> >> >>> [...] >> Mattias > That made it work! Thank you. (Where is this documented?) In CheckSynchronize itself, but not in the wiki multithread tutorial. I added it there. Mattias

Cool! Thanks again.

Now back to TFPTimer. I got it working with a hack in TFPTimerThread.Execute:

As implemented it calls Synchronize(@T.Timer). This does *not* work. In desperation I hacked it to call Queue(@T.Timer) and it works perfectly. I understand the difference between Synchronize and Queue, but I sure don't know why Queue works and Synchronize doesn't in this case. (Only on the PI, I didn't try this on Linux Mint).

Don

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160721/75e2be57/attachment.html>

Reply by email

Michael Schnell
to Donald Ziesig
Donald Ziesig wrote

TThread.Queue simply schedules an event in the Main Thread queue (i.e. an event handler procedure will be called when the main thread next time calls CheckSynchronize() ) .

TThread.Synchronize additionally stalls the thread that calls TThread.Synchronize() until the main thread is done with executing the scheduled event handler procedure.

-Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160722/a2743994/attachment.html>

Reply by email

Donald Ziesig
to Michael Schnell
Michael Schnell wrote 13 lines
On 07/21/2016 11:37 PM, Donald Ziesig wrote: > TThread.Queue simply schedules an event in the Main Thread queue (i.e. an event handler procedure will be called when the main thread next time calls CheckSynchronize() ) . TThread.Synchronize additionally stalls the thread that calls TThread.Synchronize() until the main thread is done with executing the scheduled event handler procedure. -Michael

Hi Michael,

I know the difference between Synchronize and Queue.

The problem is that Synchronize appears to suspend the main thread (but only when used in TFPTimer). I have test programs that show that TThread.Synchronize properly suspends the calling thread and executes the callback using the main thread when TThread is used by itself.

On the other hand, when TThread is subclassed to be the TFPTimerThread, Synchronize appears to suspend the main thread and the program hangs. The thread ids are all appropriate (Synchronize is called from the timer thread) but, once called, Synchronize never returns and the callback is not executed. When Synchronize is replaced by Queue, the timer program works as expected (the timer thread continues execution immediately after Queue returns, instead of waiting for the callback routine to finish).

Both cases behave identically on the Raspberry Pi and Linux Mint so I believe that the problem lies in the FPC code, not the OS.

This does not make any sense to me, but, since the behavior of TFPTimer (as modified to use Queue) is appropriate for my application, and I do have a deadline, I will use the modified TFPTimer and return to this weirdness when my project is done.

Thanks,

Don

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160722/dac49d13/attachment.html>

Reply by email