On 13/12/2018 19:52, Yuriy Sydorov wrote:
On 12/13/2018 1:06 PM, Martin wrote:
Is there a way to use FreeOnTerminate other that setting it in the constructor (or before the thread starts / or in the rather complex manner below)?

The doc does not mention any limitations https://www.freepascal.org/docs-html/rtl/classes/tthread.freeonterminate.html

However, setting FreeOnTerminate*after* Execute() has finished has no effect.

The question is why you need to set FreeOnTerminate after starting a thread? FreeOnTerminate is designed for threads which you start and forget about them because accessing the thread object is dangerous for such threads. If you need to access the thread object or control the thread's lifetime never use FreeOnTerminate.
To stop such thread use the code like this:

t.Terminate;
tm:=GetTickCount64;
while not t.Finished do
  begin
    sleep(10);
    // Check for timeout, the thread does not respond
    if GetTickCount64 - tm > 10000 then
      begin
        // Do something such as try to kill the thread
        break;
      end;
  end;

if t.Finished then
  t.Free;
The thread may makes calls (several, one after the other) into a library, and each of those calls may not return for some time (to long for the main thread to wait, without the app becoming unresponsive). And the structure of that library may not be possible to change.

The main thread launches the thread as a request that it will need the data. The main thread will have several different such workers (or other sources, that will return a result after some time).

Normally the main thread will collect all the info. But if (due to user abort, or error in one info-source) the info is no longer required, then the request should be aborted.

Of course the main thread could keep all the pending-abort thread objects, wait for them to stop, and deal with it. That however is much more work, given that depending on what happens next, the entire infra structure of the main thread may need to be freed, or may need to be used for the next request (and then would need extra space to hold the pending-abort).

Since a thread can destroy itself, it is reasonable to make use of it. Once it is no longer needed, it can be abandoned.

----
Besides, the documentation does not say that FreeOnTerminate is limited to be used in the thread construction. Especially since its effect is not due until "terminate"

Further the name does *not* indicate, if "OnTerminate" means when "execute" exits (the thread actually terminates), or it means when "terminate" is called, or both.

The  documentation also does *not* say, that "FreeOnTerminate" is not thread safe (on a method of TThread you would expect thread safety, unless otherwise documented). Yet if FreeOnTerminate is used while the thread is running, the result is unpredictable....

I am not saying that FreeOnTerminate has to be changed (I am asking if it might be useful to change it).
But if not changed, then probably the doc, wants to be much more specific.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to