我有点困惑从 c++ 中的异步函数返回大数据的正确方法是什么。
以这段代码为例。它在函数中创建一个大向量并返回分配的向量。
#include <unistd.h>
#include <iostream>
#include <chrono>
#include <future>
#include <vector>
using timepoint = std::chrono::time_point<std::chrono::system_clock>;
timepoint start_return;
std::vector< int > test_return_of_large_vector(void)
{
std::vector< int > ret(100000000);
start_return = std::chrono::system_clock::now();
return ret; // MOVE1
}
int main(void)
{
timepoint start = std::chrono::system_clock::now();
auto ret = test_return_of_large_vector();
timepoint end = std::chrono::system_clock::now();
auto dur_create_and_return = std::chrono::duration_cast< std::chrono::milliseconds >(end - start);
auto dur_return_only = std::chrono::duration_cast< std::chrono::milliseconds >(end - start_return);
std::cout << "create & return time : " << dur_create_and_return.count() << "ms\n";
std::cout << "return time : " << dur_return_only.count() << "ms\n";
auto future = std::async(std::launch::async, test_return_of_large_vector);
sleep(3); // wait long enough for the future to finish its work
start = std::chrono::system_clock::now();
ret = future.get(); // MOVE2
end = std::chrono::system_clock::now();
// mind that the roles of start and start_return have changed
dur_return_only = std::chrono::duration_cast< std::chrono::milliseconds >(end - start);
dur_create_and_return = std::chrono::duration_cast< std::chrono::milliseconds >(end - start_return);
std::cout << "duration since future finished: " << dur_create_and_return.count() << "ms\n";
std::cout << "return time from future: " << dur_return_only.count() << "ms\n";
return 0;
}
对我来说,这打印
create & return time : 543ms
return time : 0ms
duration since future finished: 2506ms
return time from future: 14ms
// ^^^^^^
所以显然,在主线程中调用函数时,返回值省略或移动就完成了。但是未来的返回值显然被复制了。此外,在尝试使用std::move
标记的行时MOVE[1,2]
,调用的返回时间future.get()
保持不变。另一方面,当返回一个指针时,返回时间future.get()
可以忽略不计(我是 0ms)。
那么大数据必须通过指针从期货返回吗?