Is it safe to use TThread.WaitFor when TThread.FreeOnTerminate is set? lazarus / 21-General ---------------------------------------------------------------------- Kurt · 2023-10-31 21:35 Is it safe to terminate a tthread that has FreeOnTerminate set and then wait for it? I am using TThread.ExecuteInThread, which returns a pointer to the TSimpleThread that it creates for this.  This is just a small prefab TThread descendent with FreeOnTerminate set.  In the example for ExecuteInThread, it uses WaitFor() on that TSimpleThread.  Is that safe?  If it is, then how? If I terminate that thread from my main thread, and it actually ends before the main thread reaches the WaitFor, then won't the TThread object already be gone? Is this just a case of "that probably hardly ever happens so don't worry about it'?  Do people normally just sleep the thread before leaving the execute? ---------------------------------------------------------------------- eljo · 2023-11-01 05:53 as far as I can tell, no its not safe. You either use the waitfor mechanism or the FreeOnTerminate, not both. ---------------------------------------------------------------------- cdbc · 2023-11-01 06:21 Hi In TThread, if you set "FreeOnTerminate", it will, as the last thing in the threadfunc, call "EndThread(0);" which then [joins] the thread. This is done, so you don't have to "Waitfor" the thread. According to posix specs it is either "EndThread" or "Waitfor". Edit: If you introduce a "Sleep" before you exit the "Execute" method, you'll just prolong the "Waitfor". Regards Benny ---------------------------------------------------------------------- alpine · 2023-11-01 10:04 Please, consult that thread: https://forum.lazarus.freepascal.org/inde x.php/topic,63482.msg480978.html#msg480978 It's rather long, but it contains useful information about the case with FreeOnTerminate=true. ---------------------------------------------------------------------- Remy Lebeau · 2023-11-03 16:36 Quote from: Kurt on October 31, 2023, 10:35:19 pmIs it safe to terminate a tthread that has FreeOnTerminate set and then wait for it? Terminate, MAYBE - IF you are careful to synchronize the termination in such a way that you won't try to terminate a TThread that has already been (or is in process of being) destroyed. Wait for it, ABSOLUTELY NOT - As soon as the Terminated flag is set, the thread can end itself at ANY time. Calling WaitFor() requires the TThread object, which will be destroyed as soon as the thread ends. So, between the time you call Terminate() and call WaitFor(), the TThread object may have already been destroyed. And if it doesn't, it WILL be destroyed while you are still inside of WaitFor(). When calling a method on an object, the object must remain alive until the method fully exits, otherwise the behavior is undefined. Quote from: Kurt on October 31, 2023, 10:35:19 pmIn the example for ExecuteInThread, it uses WaitFor() on that TSimpleThread.  Is that safe? No.  The example is wrong.  The whole point of setting FreeOnTerminate=True is to create a new thread and forget about it.  The moment you care about the thread's lifetime, FreeOnTerminate should be set to False instead so you can then manage the lifetime yourself. Quote from: Kurt on October 31, 2023, 10:35:19 pmIf I terminate that thread from my main thread, and it actually ends before the main thread reaches the WaitFor, then won't the TThread object already be gone? Yes, exactly. Quote from: cdbc on November 01, 2023, 07:21:22 amIn TThread, if you set "FreeOnTerminate", it will, as the last thing in the threadfunc, call "EndThread(0);" which then [joins] the thread. This is done, so you don't have to "Waitfor" the thread. According to posix specs it is either "EndThread" or "Waitfor". I think you misunderstood the situation.  The problem is not with the thread waiting on itself.  It is about other threads waiting on it. ---------------------------------------------------------------------- cdbc · 2023-11-03 17:51 Hi QuoteI think you misunderstood the situation.  The problem is not with the thread waiting on itself.  It is about other threads waiting on it.No I understand it quite well, just got my wording screwed up, that's all. What I quote you for, is exactly what I mean  Regards Benny