3

在 VS2015 的以下代码中,我进入acefbd了第一行,这是正确的。但在第二次测试中,我将其分成单独的行,输出为abcdef.

这是预期的行为吗?

#include <future>
#include <iostream>

using namespace std;

void a () {
std::cout << "a";
std::this_thread::sleep_for (std::chrono::seconds (3));
std::cout << "b";
}

void c () {
std::cout << "c";
std::this_thread::sleep_for (std::chrono::seconds (4));
std::cout << "d";
}

void e () {
std::cout << "e";
std::this_thread::sleep_for (std::chrono::seconds (2));
std::cout << "f";
}

int main () 
{
    std::async (std::launch::async, a), std::async (std::launch::async, c),  std::async (std::launch::async, e);

cout << "\n2nd Test" << endl;

std::async (std::launch::async, a);
std::async (std::launch::async, c);
std::async (std::launch::async, e);

}
4

1 回答 1

8

这与 Visual Studio 无关,而是您对返回的std::future对象所做的事情。std::async

当一个std::future对象被破坏时,析构函数 在等待未来准备就绪时阻塞。

第一行发生的情况是您创建了三个未来对象,并且在完整表达式的末尾(在您创建最后一个之后)未来超出范围并被破坏。

在“第二次测试”中,您创建了一个在继续之前必须被破坏的未来,然后您创建另一个在继续之前必须被破坏的未来,最后是第三个当然也必须被破坏的未来。

如果您将第二个测试中的期货保存在临时变量中,您应该会得到相同的行为:

auto temp_a = std::async (std::launch::async, a);
auto temp_c = std::async (std::launch::async, c);
auto temp_e = std::async (std::launch::async, e);
于 2017-07-13T07:03:15.280 回答