直接上代码
class thread_guard
{
private:
std::thread& t;
public:
explicit thread_guard(std::thread& _t):t(_t){}
~thread_guard()
{
if(t.joinable())
t.join();
}
private:
thread_guard(thread_guard const&);
thread_guard& operator=(thread_guard const&);
};
void hello()
{
std::cout<<"hello"<<std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
for(int k = 0 ; k < 10 ; k++)
{
//std::auto_ptr<std::thread> test(new std::thread(hello));
//boost::shared_ptr<std::thread> te(new std::thread(hello));
std::thread test(hello);
std::thread te(hello);
thread_guard g(test);
thread_guard ge(te);
}
return 0;
}