作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
规范 (? - 从 cppreference 得到它) 指出:
~thread(); (since C++11)
Destroys the thread object. If *this still has an associated running thread (i.e. joinable() == true), std::terminate() is called.
std::terminate()
内部线程中止整个程序。
#include <iostream>
#include <thread>
#include <memory>
int main() {
std::unique_ptr<std::thread> thread_ptr(new std::thread([](){
std::cout << "Starting thread: " << std::endl;
while(1) {}
}));
while(!thread_ptr->joinable()){}
std::cout << thread_ptr->joinable() << std::endl;
thread_ptr.release();
std::cout << "Main is still alive!" << std::endl;
return 0;
}
1Starting thread:
Main is still alive!
最佳答案
您的意思可能是 thread_ptr.reset()
而不是 thread_ptr.release()
. release()
放弃指针的所有权,即您泄漏了 std::thread
因此永远不会调用实例及其析构函数。
关于multithreading - 可连接 std::thread 的析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25911734/
我是一名优秀的程序员,十分优秀!