Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-11

CloseThread needed? still unclear

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

While testing an updated version of nYume (aservia) I found that the program is leaking thread handles on Ms Windows. The program uses BeginThread and EndThread calls.

However, when I add CloseThread() right before EndThread(, the program no longer leaks handles. So, CloseThread may be needed on Ms Win machines. I don't think (all) the docs state to call closethread. If CloseThread should be called on Ms windows, then this makes cross platform threading difficult since windows requires something different than unix.

I don't know if the object oriented TThread uses CloseThread in it's backend code and don't know if TThread leaks, I was just trying beginthread and endthread style.

To find handle leaks, I simply use Windows Task Manager to monitor the processes, with the Handles column visible. I am also trying some other tools like DrMemory.

First I thought I would post this to the mailing list to ask you what the proper way to program with threads is. If we must call closethread on MS Win machines but not unix machines, then something needs to change to make the threading in the RTL more cross platform (and all the docs would need to mention things about closethread if it is needed).

The compiler is 2.4.2 and 2.4.4 that i am using.

The documentation is here:
http://www.freepascal.org/docs-html/prog/progse44.html#x217-23000010.2

But there is also more documentation elsewhere, such as the RTL docs.

I guess I could look into Delphi to see how they use closethread. in their backend somewhere.

Sven Barth
to Unknown author

Am 14.11.2011 02:32, schrieb

Show quoted text 5 lines
First I thought I would post this to the mailing list to ask you what the proper way to program with threads is. If we must call closethread on MS Win machines but not unix machines, then something needs to change to make the threading in the RTL more cross platform (and all the docs would need to mention things about closethread if it is needed).

The bug was already mentioned here
http://mantis.freepascal.org/view.php?id=13160 and fixed for 2.2.2. "CloseThread" was implemented in a cross platform way.

Regards,
Sven

Unknown author
to Sven Barth
Show quoted text 16 lines
Am 14.11.2011 02:32, schrieb > First I thought I would post this to the mailing list to ask you what > the > proper way to program with threads is. If we must call closethread on MS > Win machines but not unix machines, then something needs to change to > make > the threading in the RTL more cross platform (and all the docs would > need > to mention things about closethread if it is needed). The bug was already mentioned here http://mantis.freepascal.org/view.php?id=13160 and fixed for 2.2.2. "CloseThread" was implemented in a cross platform way. Regards, Sven

Hmm, but the compiler I'm using right now is a 2.4 and I think there is a leak if I don't call closethread. CloseThread was implemented for EndThread or how does it get called? I think the closethread has to be called outside the thread rather than inside the thread from what I tried, but I don't know.

Sven Barth
to Unknown author
Show quoted text 19 lines
> Am 14.11.2011 02:32, schrieb >> First I thought I would post this to the mailing list to ask you what >> the >> proper way to program with threads is. If we must call closethread on MS >> Win machines but not unix machines, then something needs to change to >> make >> the threading in the RTL more cross platform (and all the docs would >> need >> to mention things about closethread if it is needed). > > The bug was already mentioned here > http://mantis.freepascal.org/view.php?id=13160 and fixed for 2.2.2. > "CloseThread" was implemented in a cross platform way. > > Regards, > Sven Hmm, but the compiler I'm using right now is a 2.4 and I think there is a leak if I don't call closethread.

That is exactly my point: CloseThread was implemented because of that leak (see the comments in the bug issue I linked to).

CloseThread was implemented for EndThread or how does it get called? I think the closethread has to be called outside the thread rather than inside the thread from what I tried, but I don't know.

Yes, it must be called outside of your thread function. See also: http://www.freepascal.org/docs-html/rtl/system/closethread.html

If you still have a leak then you might want to post a little example showing the problem.

Regards,
Sven

Unknown author
to Sven Barth
Show quoted text 26 lines
On 28.11.2011 21:25, wrote: >> Am 14.11.2011 02:32, schrieb >>> First I thought I would post this to the mailing list to ask you what >>> the >>> proper way to program with threads is. If we must call closethread on >>> MS >>> Win machines but not unix machines, then something needs to change to >>> make >>> the threading in the RTL more cross platform (and all the docs would >>> need >>> to mention things about closethread if it is needed). >> >> The bug was already mentioned here >> http://mantis.freepascal.org/view.php?id=13160 and fixed for 2.2.2. >> "CloseThread" was implemented in a cross platform way. >> >> Regards, >> Sven > > Hmm, but the compiler I'm using right now is a 2.4 and I think there is > a > leak if I don't call closethread. That is exactly my point: CloseThread was implemented because of that leak (see the comments in the bug issue I linked to).

I see. I think I found some documentation that still doesn't include CloseThread in the examples.. I will send patches or recommendations to the documentation soon. For example closethread is now documented as a function, but some of the other pages that show threading examples just use endthread. I thought maybe closethread somehow was tied into endthread but this not the case.

Unknown author
to Unknown author
Show quoted text 8 lines
I think I found some documentation that still doesn't include CloseThread in the examples.. I will send patches or recommendations to the documentation soon. For example closethread is now documented as a function, but some of the other pages that show threading examples just use endthread. I thought maybe closethread somehow was tied into endthread but this not the case.

For example:
http://www.freepascal.org/docs-html/prog/progse44.html#x217-23000010.2

In this example it doesn't call closethread (and it doesn't use endthread).