#include <iostream> #include <thread> using namespace std; class TyThread { public: TyThread(std::thread &t) :m_thread(t) { } ~TyThread() { if (m_thread.joinable()) { m_thread.join(); } } TyThread(const TyThread &o) = delete; TyThread &operator=(const TyThread &o) = delete; protected: private: std::thread &m_thread; }; void Test() { cout << "进入Test()\n"; this_thread::sleep_for(chrono::seconds(10)); cout << "Test():hello world!\n"; } void palyx() { std::thread t(Test); TyThread tythread(t); } void main() { palyx(); system("pause"); }
结果: