<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 14/12/2018 10:30, Michael Van
Canneyt wrote:<br>
</div>
<blockquote type="cite"
cite="mid:alpine.DEB.2.20.1812141026010.6854@home.telenet.be">
<blockquote type="cite">(or as Martin says, it should be
documented as such?)
<br>
</blockquote>
<br>
This is a valid point and I intend to elaborate the TThread
documentation
<br>
somewhat, with this in mind.
<br>
</blockquote>
<br>
I did have an error in my initial example:<br>
<blockquote type="cite">
<pre> t := TThread.create(false); // create not suspended
// do something
if foo then begin // decide we do not need the result
t.FreeOnTerminate:= true;
t.terminate;
end;</pre>
</blockquote>
Obviously "t.terminate" is not save after "t.FreeOnTerminate:= true;
" because by that time t may be dangling. The only thing save to do
with "t" after at that point is "t:=nil".<br>
<br>
But that aside, I think there are 2 thinks that can be made part of
the documentation (they may be obvious, but "obvious" here is a
relative term, depending on the readers experience)<br>
<br>
1)<br>
A thread that has its FreeOnTerminate property set to "True" must
not be accessed in any for by any other thread. <br>
<br>
An exception to this rule, is access to the thread from inside a
synchronize event, that comes from the very thread itself. This is
because the while the synchronize event is executed the thread is on
hold and can not be destroyed.<br>
<br>
[optional]<br>
This is also true for any thread that at any time had its
FreeOnTerminate property set to "True", and then reverted it back to
false (from within the thread itself, as this the thread itself is
the only place that is still allowed access).<br>
Again the exception is that the property can be reverted inside a
synchronize event, in which case the thread can be accessed again
after that.<br>
<br>
2)<br>
The FreeOnTerminate property is evaluated once the thread's Execute
method is exited. This is *before* "procedure DoTerminate; virtual;"
and the "OnTerminate: TNotifyEvent" are executed. [[According to
google/stackoverflow, this is the same in Delphi]]<br>
<br>
Document relation to property "Finished" / see below<br>
<br>
----<br>
For (2) I have a question/request <br>
<br>
The current FPC implementation (3.0.4) is <br>
FreeThread := Thread.FFreeOnTerminate; // property is evaluated
/ can no longer be changed<br>
Result := Thread.FReturnValue;<br>
Thread.FFinished := True; // Marked as finished<br>
Thread.DoTerminate;<br>
if FreeThread then<br>
Thread.Free;<br>
<br>
Could "Thread.FFinished := True;" be moved to the top? (potentially
together with "Result := Thread.FReturnValue;" so the order of
those 2 remains).<br>
<br>
The reason is, that then (on a suspended thread) it would be save to
change "FreeOnTerminate" if "Finished = False".<br>
<br>
This should be ok, the doc
<a class="moz-txt-link-freetext" href="https://www.freepascal.org/docs-html/rtl/classes/tthread.finished.html">https://www.freepascal.org/docs-html/rtl/classes/tthread.finished.html</a>
explicitly states that when it is true "the thread is still cleaning
up (calling OnTerminate, etc)."<br>
<br>
<br>
</body>
</html>