std::thread我在 Bjarne Stroustrup 的《C++ 编程语言》中看到了一个基本用法的示例,它让我感到困惑。这是示例:
void run(int i, int n) // warning: really poor code
{
thread t1 {f};
thread t2;
vector<Foo> v;
// ...
if (i<n)
{
thread t3 {g};
// ...
t2 = move(t3); // move t3 to outer scope
}
v[i] = Foo{}; // might throw
// ...
t1.join();
t2.join();
}
Run Code Online (Sandbox Code Playgroud)
正如斯特鲁斯特鲁普所写:
我们可能永远不会到达
join()最后的 2。在这种情况下,析构函数t1将终止程序。
但是,在什么情况下t1调用 的析构函数呢?线程t1不会超出其范围。也没有明确的delete调用。
我尝试通过适当的更改来运行此代码,但仍然找不到如何t1调用 的析构函数。