[fpc-pascal] RTL Thread.ExecuteInThread interface and implementation wrong

Michael Van Canneyt michael at freepascal.org
Wed Mar 21 10:47:01 CET 2018



On Tue, 20 Mar 2018, Anthony Walter wrote:

> Okay, but the problem with the current implementation in Classes.pas is
> that Status cannot be used, Terminated cannot be checked, and there is no
> access to Synchronize.

All wrong. See below.

> As such OnStatus is pointless, and the semantics of
> checking Terminated doesn't work. If that's how the these
> ExecuteInThread class methods on TThread are going work then they should be
> removed because they don't have significant function and likely would
> confuse users.
>
> Eg. of confusion when trying to use ExecuteInThread Q: How does OnStatus
> work? A: It doesn't.

And why doesn't it ?

You must obviously pass a callback. Just as in asynchronous programming. 
Of course your function must call it from time to time.

> Q: How can I check if my thread method is terminated?

By passing aOnTerminate ?

If you mean inside the thread, use TThread.CheckTerminated. 
This is a class function.

or use TThread.CurrentThread.Terminated;


> A: Forget about Thread.Terminated, you'll need to set a flag somewhere or
> create you own solution. Q: How can I suspend my method and safely join the
> main thread, because I normally use Synchronize? A: You'll have to do
> without Synchronize or maybe figure out a workaround.

You can perfectly call Synchronize. It is a class method:

Procedure MyThreadHandlerFunction ;

begin
   TThread.Synchronize(CurrentThreead,SomeMethod);
end;

>
> So IMO ExecuteInThread should either be removed or these problems should be
> fixed.


IMO you should check the documentation a little more thorough.

https://www.freepascal.org/docs-html/current/rtl/classes/tthread.executeinthread.html

It contains 4 examples that show how things work.

Michael.



More information about the fpc-pascal mailing list