标签:let section 子线程 分享 sys 程序 git ima vat
void f(){
std::thread t(my_func);
try{
do_some_work();
}
catch{
t.join();
throw;
}
t.join();
}
#include <iostream>
#include <thread>
using namespace std;
class my_thread{
public:
explicit my_thread(thread& t_):t(t_){}
~my_thread(){
if(t.joinable()){ // -------->①
t.join();// -------->②
}
}
my_thread(my_thread const&) = delete;// -------->③
my_thread& operator=(const my_thread&) = delete;
private:
thread& t;
};
class func{
public:
int& data;
func(int& d):data(d){}
void operator()(){
cout << "thread started@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
for(unsigned j = 0; j < 100; ++j){
cout << j << endl;
}
}
};
int main(){
int state = 0;
func f(state);
thread t(f);
my_thread mt(t);
//do_something_in_current_thread();
}// -------->④
terminate called after throwing an instance of ‘std::system_error‘
what(): Invalid argument
Aborted (core dumped)
标签:let section 子线程 分享 sys 程序 git ima vat
原文地址:https://www.cnblogs.com/xiaoshiwang/p/9752001.html