包装类对象析构时执行线程
#include <iostream>
#include <thread>
struct thread_guard
{
public:
std::thread& t;
explicit thread_guard(std::thread& _t) : t(_t) {};
~thread_guard()
{
if(t.joinable())
t.join();
}
thread_guard(thread_guard&) = delete;
thread_guard& operator= (thread_guard const &) = delete;
};
int main()
{
std::thread t([] {
std::cout << "bye\n";
});
thread_guard g(t);
for (int i = 0; i < 12; i++)
std::cout << i << std::endl;
return 0;
}