c ++如何使我.get() std::future out of a vector.
c++ How do I .get() std::future out of a vector?
这是我的 c++ 类最终项目。 我第一次尝试使用 std::future,需要一些帮助。 你可以在这里看到我的所有代码:https://github.com/AdamJHowell/3370-FinalProject
这是我试图从中获得未来的函数的签名:
std::vector<stockDay> FuturesSmaEma( std::size_t numP, std::vector<stockDay> &inputVector );
我在此块中尝试了多种解决方案:
std::vector<std::future<std::vector<stockDay> > > futureSpawnVector; // A vector of futures that we are spawning.
std::vector<std::vector<stockDay> > futureResultsVector; // A vector of futures return values.
for( auto inputVector : VectorOfStockDayVectors ){
futureSpawnVector.push_back( std::move( std::async( FuturesSmaEma, numPArray[0], inputVector ) ) );
}
for each ( auto &var in futureSpawnVector ){
var.wait();
}
for each ( auto &var in futureSpawnVector ){
// Put the future from that into our futureResultsVector.
futureResultsVector.push_back( var ); // Produces Error C2280 'std::future<std::vector<stockDay,std::allocator<_Ty>>>::future(const std::future<std::vector<_Ty,std::allocator<_Ty>>> &)': attempting to reference a deleted function 3370-FinalProject f:program files (x86)microsoft visual studio 14.0vcincludexmemory0 637
futureResultsVector.push_back( std::move( var ) ); // Produces Error C2664 'void std::vector<std::vector<stockDay,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>::push_back(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'const std::future<std::vector<stockDay,std::allocator<_Ty>>>' to 'std::vector<stockDay,std::allocator<_Ty>> &&' main.cpp 179std::allocator<std::vector<stockDay, std::allocator<stockDay>>>>
futureResultsVector.push_back( var.get() );
futureResultsVector.push_back( std::move( var.get() ) );
auto tempStuff = var.get(); // Produces Error C2662 'std::vector<stockDay,std::allocator<_Ty>> std::future<std::vector<_Ty,std::allocator<_Ty>>>::get(void)': cannot convert 'this' pointer from 'const std::future<std::vector<stockDay,std::allocator<_Ty>>>' to 'std::future<std::vector<stockDay,std::allocator<_Ty>>> &'
std::cout << "futureSpawnVector now has " << tempStuff.size() << " elements." << std::endl;
std::cout << tempStuff[0].date << " ema: " << tempStuff[0].ema << std::endl;
std::cout << var.get().at( 0 ).date << " ema: " << var.get()[0].ema << std::endl; // Desperate attempt to understand what is going on here.
std::vector<std::future<std::vector<stockDay> > > smaVector42;
futureResultsVector.push_back( smaVector42 ); // Produces Error (active) no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::future<std::vector<stockDay, std::allocator<stockDay>>>, _Alloc=std::allocator<std::future<std::vector<stockDay, std::allocator<stockDay>>>>]" matches the argument list
}
我希望这个块能从这些线程创建结果向量。 我做错了什么?
我还看过这里:如何将期货放入容器中?
我有一个返回"stockDay"类对象的向量的函数。 向量中的每个元素表示一天的库存数据。
我需要运行此功能 15 次(在 3 个周期内的 5 只股票上运行)。 所以我想线程化该函数,并获取返回的向量。
我可以轻松地创建一个单一的未来,等待线程(),并获取()结果向量。 但是,我想创建这些未来的向量。 如果我理解正确,这将是期货的向量,每个期货都是"stockDay"类对象的向量。
我似乎让该部分正常工作。 向量 .size() 显示正确数量的元素(现在有五个用于测试)。
我遍历该向量,并对每个元素执行 .wait()。 那也行得通。
现在我想.push_back() 每个未来的 .get() 到另一个向量中。 这对我不起作用。
我知道您希望我为您提供确切的错误消息,但是我已经尝试了大约十几种不同的解决方案,它们都会产生略有不同的错误。
第174 行到第 187 行是发生错误的位置。 我把它们注释掉了,不同的尝试彼此分开。
我理解此处显示的代码:https://solarianprogrammer.com/2012/10/17/cpp-11-async-tutorial/
您将在我的代码第 135 到 148 行中看到该教程的示例。
futureResultsVector.push_back( var ); // Produces Error
你不能复制future
,你需要移动它:
futureResultsVector.push_back( std::move(var) );
auto tempStuff = var.get(); // Produces Error
该错误意味着var
是一个 const 对象,您无法从 const 未来中获取结果。您需要修复代码,以便它不会在 const 上下文中访问它。
futureResultsVector.push_back( smaVector42 ); // Produces Error
您正在尝试将向量推入向量。这显然是行不通的。
如果您尝试push_back()
future
,那是您的错误,因为future
没有复制构造函数,因此无法将现有构造函数推回。
相反,您应该创建一个vector<future>
容器并push_back()
std::async
如下操作:
#include <iostream>
#include <vector>
#include <future>
using namespace std;
int func(int arg, int arg2)
{
return arg + arg2;
}
int main()
{
vector<future<int>> tasks;
future<int> moveThis;
tasks.push_back(async(func, 1, 2));
}
您还可以通过使用std::move()
移动未来对象来push_back()
它:
tasks.push_back(std::move(moveThis));
那么从那个未来得到答案应该很简单:
int temp = tasks[0].get();
注: 使用 get()
后,将来的对象将变为无效,您无法再次get()
它。
由于Microsoft的C++/CLI(https://en.wikipedia.org/wiki/C%2B%2B/CLI)。 Visual Studio 2015允许这样的循环...
for each( auto var in container )
。但它不会编译某些类型,例如期货向量。
要将 future 放入向量中,使用 std::future.push_back(),作为:
std::vector<std::future<T>> futures[num_blocks - 1];
futures[i].push_back(SOMETHING_FUTURE);
或者从向量中获取未来,使用 (*std::future.begin()).get(),但只有一次 .get() 可以使用。如:
T result =(*futures[i].begin()).get();
这可能有所帮助,或者可能是分段错误。和平!
- 架构决策:返回std::future还是提供回调
- 使用用户定义的参数调用future/async并调用类方法
- IN, OUT, INOUT Parameters
- 即使使用调试编译标志,表达式也是"optimized out"
- C++双重释放或损坏(out)
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- std::p romise::set_value() 和 std::future::wait() 是否提供内存围栏?
- 修改在 std::future 的 lambda 中引用捕获的值
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 从 T 创建 std::future 的最佳方式<T>
- C++ std::future没有被调用
- 解决方案在第 25 行执行错误'out of bounds'
- C++ 向量加减抛出"expression: vector subscript out of range."错误
- 对于等待以 std::future wait() 返回的函数的 CPU 使用率或检查标志在循环中休眠一段时间哪个更好?
- 对函数的 out 字符串参数使用 swap 与赋值
- 如何使用 boost::future 重新抛出 std::exception_ptr 存储的原始异常?
- 运行编译的 a.out 后出现分段错误(核心转储)错误
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- c ++如何使我.get() std::future out of a vector.