我想运行一个线程,做这样简单的东西:终止正在运行的线程C++的std ::线程
main(){
std::thread thread_pulse([=]{
*this->do_adress = true;
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
*this->do_adress = false;
//delete this thread to avoid memory leaks
});
//do some other stuff without waiting for the thread to terminate
}
我怎么保证,当线程执行完成的线程删除,并且没有内存泄漏而不等待线程在main上完成执行?
编辑:
感谢您的帮助,蒙山这个工作,因为我想
main(){
std::thread ([=]{
*this->do_adress = true;
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
*this->do_adress = false;
//delete this thread to avoid memory leaks
}).detach;
//do some other stuff without waiting for the thread to terminate
}
'std :: thread thread_pulse(...); thread_pulse.detach();' –