Rob Caldecott wrote:
I have just started to investigate the boost::thread library and have a couple of questions (Win32/VS2005):
1. I have an application that creates lots of relatively short-lived threads. When a thread is created I store the handle in an array, and at regular intervals, I remove completed threads from the array by checking the thread state via ::GetExitCodeThread. The application runs 24/7 so I don't want an array containing thousands of "dead" handles.
I thought I'd replace this code with a boost::thread_group, but I cannot see any way to remove completed threads from the internal list. This means the std::list used by boost::thread_group would keep on increasing in size until the application exits, which I want to avoid. If I decide to roll my own thread group code, how can I check if a boost::thread is still active or not?
You can't. Interesting timing; I submitted a proposal for a threading API to the C++ committee: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2090.html that lets you do that. You can keep a vector< thread::handle > and periodically sweep it using thread::try_join as a predicate. I've been having troubles convincing some committee members of its utility, though. That said, why do you even have to keep the boost::thread objects? There aren't many useful things that you could do with a boost::thread; if you run 24/7 you should be able to just spawn the threads "detached": boost::thread( f ); without keeping them in a thread group.
2. I am getting a memory leak when my application exits, which has already been mentioned here. Does anyone have a fix for this which I can apply to the 1.33.1 source (then rebuilding the thread lib by hand)? It would be much appreciated. I am also unsure if this leak will be fixed for 1.34 - does anyone know for certain?
You can replace your version of tss_hooks.cpp with this one: http://boost.cvs.sourceforge.net/*checkout*/boost/boost/libs/thread/src/tss_... and rebuild. 1.34 does contain a fix for that too, albeit a different one.