How to clean up thread automatically

tinyabs

Member
Mar 8, 2003
158
0
0
I encounter a problem of having to cleanup threads handle after threads terminates.

I implemented IO threads to take care of my IO tasks. Whenever the threads terminate, I should close the handles of these threads to prevent accumulation of dead/signaled handles. Right now I use a deque (STL) object to track these handle. I test the signal using WaitForSingleObject(hThread, 0) and call CloseHandle() if it was signaled (terminated). Although it works, it is not a graceful method.

I did try system thread pool but encountered thread count limits (64 max). I know I'm shouldn't be using many threads but this particular system uses pipe. So 64 or more active pipe is possible (without connection pooling).

I need a way to close these handle automatically after the threads are terminated. A simple method is the best. Anyone had a trick or two to share?