@LeP
Waitfor can cause the same race condition as Marco described: it is not an improvement in combination with calling terminate on a thread from another thread.
It IS an improvement if a thread itself knows it is done. That is about the same as per PascalDragon's comment above in the sense that the thread itself needs a mechanism to know it should finish and that either takes extra code or the thread itself knows it has finished its job (designed as finite, not infinite).
I know. But I was referring to the fact that waitfor is internal to the thread's "terminate" routine (ThreadShutdown in Delphi).
Regarding FreeOnTerminate, as stated in the documentation, if a thread has a FreeOnterminate property set to True, it cannot be accessed under any circumstances after creation (or at startup if the creation state is suspended).
So there can't be any problem: the thread shouldn't be accessed (in fact, it MUST NOT be accessed) because it can "disappear" at any time after creation or startup.
You can't use Free, Terminate, WaitFor, or any other property. Only the thread's user code can decide whether and how to exit, and if the rules are followed, there shouldn't be any problems.
Is the thread blocking? That's a programming error, not a design error.
And if freeonterminate is FALSE, in Delphi the THREAD can always be terminated with a FREE, without any further action, even in suspended state. Except when the thread is in a waiting state (as far as I know) and that can be the issue (if one don't use the right way).
As for states, it's not possible to correctly manage states from user code/internal class code/operating system code anyway.
Three different entities can manage the same object in a non-cooperative manner: rules must be found and applied, there are no alternatives.
And the very fact of needing to know the FreeOnTerminate property is a contradiction: how can one know this property if, when that is true, one cannot access the Thread (externally)?