gpt4 book ai didi

c++ - 异步 C++11 中 future 的析构函数

转载 作者:太空狗 更新时间:2023-10-29 20:05:24 25 4
gpt4 key购买 nike

编译器不应该调用 future 的析构函数futuremain 之后完成,也就是说,不应该是函数 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com