作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
编译器不应该调用 future 的析构函数future
在 main
之后完成,也就是说,不应该是函数 f()
无论如何打电话? (gcc 4.7.2 不这样做)。
#include <iostream>
#include <thread>
#include <future>
using namespace std;
void f() {
cout << "thread...\n";
}
int main() {
auto future = async(&f);
cout << "I am main\n";
}
编辑:我只 Hello from main
.正文thread...
根本不打印。
编辑 2: future 调用的析构函数是否为 wait()
??
最佳答案
注意:下面的信息已过时(C++14 之前)。请参阅 Jonas 的回答以获取最新信息。
Should not a compiler call the destructor of the future future right after the main finishes
在 main
完成之前。但是是的。
that is, should not be the function f() called anyway?
没有,为什么?是什么让您认为 std::future
的析构函数会这样做?这不是析构函数的工作。事实上,根据§30.6.6/9,析构函数的唯一功能是释放 future 的共享状态并销毁*this
。仅此而已。
关于c++ - 异步 C++11 中 future 的析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13635883/
我是一名优秀的程序员,十分优秀!