3
在VS2015中的下面的代碼中,我在第一行得到acefbd
,這是正確的。但是在第二次測試中,我將其分爲單獨的行,輸出爲abcdef
。VS2015 std ::異步奇怪
這是預期的行爲?
#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);
}
你知道以何種順序臨時工被破壞?像是它的實現定義? – Rakete1111
@ Rakete1111我假設你的意思是使用逗號表達式進行第一次測試?然後不,我不知道,也不是沒有看過規範。 –
@ Rakete1111臨時對象在同一個完整表達式中以相反的構造順序被破壞;除了由於被綁定到引用而延長了生命週期的那些。 (哪些不這樣做)。 –