我對C/C++相當陌生。我試圖在返回值時調用2個函數。我做了一些研究,並且我知道線程並不適合這一點,因爲通過線程很難獲得返回值。我正在使用未來的庫與異步這一點,但它沒有一次調用多個函數(我已經測試了一個睡眠(),我看到它等待其他函數完成獲取返回值)。請查找代碼在這裏,可以通過以簡單代碼爲例來幫助我嗎?在C++中使用返回值調用多個函數
#include <iostream>
#include <future>
#include <windows.h>
#include <string>
using namespace std;
// a non-optimized way of checking for prime numbers:
string test(string t1)
{
string h = "Hello";
string hh = h+t1;
return hh;
}
string test1(string t2)
{
string hh = "Hello";
string hhh = hh+t2;
return hhh;
}
int main()
{
string t1 = "Hai test1";
string t2 = "Hai teset2";
future<string> stt = async (test,t1);
future<string> sttt = async (test1,t2);
string re1 = stt.get();
string re2 = sttt.get();
cout << re1 << endl;
cout << re2 << endl;
return 0;
}
請[編輯](http://stackoverflow.com/posts/30916130/edit)與你的問題[SSCCE(http://sscce.org)表現出問題。 – NathanOliver
您可能需要傳遞'std :: launch :: async'來強制異步執行,但我不確定您的意思是「等待其他函數完成獲取返回值」。 – TartanLlama