[fpc-pascal] What's the deal with CloseThread?
    Sven Barth 
    pascaldragon at googlemail.com
       
    Wed Apr 24 09:53:06 CEST 2013
    
    
  
Am 23.04.2013 22:40, schrieb Anthony Walter:
> Thanks for so much Sven. I have another question.
>
> Is it okay for a thread to close itself before it's finished? Because 
> I believe that is what happens when FreeOnTerminate is set to True. 
> Here is a brief synopsis of what can execute:
>
> In rtl/objpas/classes/classes.inc
>
> Line 168: FreeThread := Thread.FFreeOnTerminate; // captures free on 
> termiante
> Line 173: Thread.Free; // invokes the destructor from within the thread
> Line 197: CloseThread(FHandle); // thread is closed in destructor 
> TThread.Destroy;
> Line 174: EndThread(Result); // flow returns here, but the thread was 
> already closed
>
> So what it looks like is that we expect code inside a thread continue 
> running after it has been closed.
>
That's ok, because the FHandle is information which is retrieved in the 
context of the calling thread (e.g. the main thread). And if we don't 
need any more information about the thread we just close it's handle 
(equivalent to CloseThread) and be done with it. EndThread on the other 
hand is called in the context of the called thread.
Afterall you also normally close the thread handle which you get 
returned by a CreateProcess(?) call, but keep the process handle around.
With CloseHandle (and the semantically equal CloseThread) you just tell 
the system that you don't plan to interact with the object that the 
handle references (in this case a thread) anymore.
Regards,
Sven
    
    
More information about the fpc-pascal
mailing list