我想運行一個線程,做這樣簡單的東西:終止正在運行的線程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();' –