www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Thread termination

reply "Kris" <fu bar.com> writes:
I notice on Win32 that the system-level handle created for each new thread
is apparently not closed properly. You can see this by slowly creating a
number of threads, and inspecting the process handle-count via Task Mgr  ~
it increments by one for each thread, but doesn't revert as each thread
terminates.

This would be become terminal for a thread-pool.
May 19 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Kris" <fu bar.com> wrote in message news:d6jugo$8ga$1 digitaldaemon.com...
 I notice on Win32 that the system-level handle created for each new thread
 is apparently not closed properly. You can see this by slowly creating a
 number of threads, and inspecting the process handle-count via Task Mgr  ~
 it increments by one for each thread, but doesn't revert as each thread
 terminates.

 This would be become terminal for a thread-pool.
If you can suggest a fix to std.thread, I'd appreciate it!
May 20 2005
next sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
I would guess the usage of:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__endthread.2c_._endthreadex.asp

It sounds like this should be called from the thread itself, yes?  It 
also says:

 when you use _beginthreadex and _endthreadex, you must close the 
thread handle by calling the Win32 CloseHandle API. Is that happening? Anyway, I guess I would add: _endthreadex(result); Before/instead of: return result; In the Windows part of std.thread's threadstart. But, erm, I haven't worked all that much with threads myself. -[Unknown]
 If you can suggest a fix to std.thread, I'd appreciate it!
May 20 2005
parent Sean Kelly <sean f4.ca> writes:
In article <d6k856$hu8$1 digitaldaemon.com>, Unknown W. Brackets says...
I would guess the usage of:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__endthread.2c_._endthreadex.asp

It sounds like this should be called from the thread itself, yes?
Assuming the thread is created via _beginthreadex, you just need to call CloseHandle on the thread handle before destruction. You can actually close the thread handle any time you want, as this just detaches the thread. Sean
May 20 2005
prev sibling parent kris <fu bar.org> writes:
Walter wrote:
 "Kris" <fu bar.com> wrote in message news:d6jugo$8ga$1 digitaldaemon.com...
 
I notice on Win32 that the system-level handle created for each new thread
is apparently not closed properly. You can see this by slowly creating a
number of threads, and inspecting the process handle-count via Task Mgr  ~
it increments by one for each thread, but doesn't revert as each thread
terminates.

This would be become terminal for a thread-pool.
If you can suggest a fix to std.thread, I'd appreciate it!
Well, you'd typically close the 'hdl' inside std.thread.threadstart(), after the delegate or function had returned. Or during the dtor. That aside, and in looking at the code, I see what appears to be potential for a race-condition between the GC and a terminating thread. When a thread terminates, it null out an entry in an array ... one which resumeAll/pauseAll might currently be inspecting ... and there's no synch. I guess you're relying on 32-bit read/write being atomic? Either way, there'd probably a race condition between CloseHandle and the usage of that handle elsewhere. Placing a CloseHandle in the Thread dtor would at least avoid a race against the GC thread (though it still might against other threads). I imagine you can't synch some of this due to deadlock potential via the GC (it pauses all threads, and then tries to get a mutex held by one of the paused threads).
May 20 2005