[英]In Visual Studio, `thread_local` variables' destructor not called when used with std::async, is this a bug?
[英]When is std::thread destructor called?
我知道在主出口上调用std::thread
析构函数,或者当一个线程对象超出作用域时。
但它正在调用的函数执行时是否也会被销毁?
如果不是这样的线程发生了什么,我还可以join()
吗?
但它正在调用的函数执行时是否也会被销毁? 如果不是这样的线程发生了什么,我还可以
join()
吗?
不,它没有被销毁,但标记为joinable()
。 所以是的,你仍然可以join()
它。
否则从问题的标题( “什么时候调用std :: thread析构函数?” )和你在帖子中说的话
我知道在主出口上调用std :: thread析构函数,或者当一个线程对象超出作用域时。
它与任何其他实例一样:当实例超出范围时调用析构函数,或者在动态分配实例时调用delete
。
这是一个小例子代码
#include <thread>
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
void foo() {
std::cout << "Hello from thread!" << std::endl;
}
int main() {
std::thread t(foo);
std::this_thread::sleep_for(1s);
std::cout << "t.joinable() is " << t.joinable() << std::endl;
t.join();
}
输出是
Hello from thread! t.joinable() is 1
现场观看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.