您可以这样做,但请注意同步(最好使用共享指针来 boost::thread_group 而不是引用,除非您确定该线程组将存活足够长的时间):
void someFunc(..., boost::thread_group & thg, boost::thread * thisTh)
{
// do sth
thg.remove_thread(thisThr);
delete thisTh; // we coud do this as thread of execution and boost::thread object are quite independent
}
void run()
{
boost::thread_group my_threads;
boost::thread *t = new boost::thread(); // invalid handle, but we need some memory placeholder, so we could pass it to someFunc
*t = boot::thread(
boost::bind(&someFunc, boost::ref(my_threads), t)
);
my_threads.add_thread(t);
// do not call join
}
您还可以检查 at_thread_exit() 函数。
无论如何, boost::thread 对象的重量不应为 30 MB。