[fpc-pascal] What's the deal with CloseThread?

Marco van de Voort marcov at stack.nl
Tue Apr 23 09:32:02 CEST 2013

In our previous episode, Anthony Walter said:
> I was looking at threading support in the System unit and also in the
> TThread class.
> According to the documentation at
> http://www.freepascal.org/docs-html/rtl/system/closethread.html
> CloseThread must be called on any thread started with BeginThread. It must
> be called after the thread has ended (either by exiting the thread function
> or after calling EndThread).

Yes, Closethread closes possible handles deallocating anything that must
remain allocated to read out thread results.

> Looking at the TThread class, which does indeed use BeginThread in
> TThread.SysCreate (tthread.inc), I see no corresponding call the
> CloseThread. I grepped the entire fpc source tree and found no references
> anywhere to any code which calls CloseThread.

_BUT_ tthread.inc's are target dependent and thus don't necessarily go
through the platform independent procedural "beginthread" interface. 
(actually, some parts of the procedural interface, including chlosethread
were only crafted _after_ tthreads completion, when it turned out that the
procedural interface had leaks, and the tthread not)

E.g. the Closehandle at rtl/win/tthread.inc:35 does the same thing.

More information about the fpc-pascal mailing list