Ada*_*ell -1 c++ multithreading vector
这是我的 C++ 类最终项目。我第一次尝试使用 std::future,需要一些帮助。你可以在这里看到我的所有代码: https //github.com/AdamJHowell/3370-FinalProject
这是我试图从中获得未来的函数的签名:
std::vector<stockDay> FuturesSmaEma( std::size_t numP, std::vector<stockDay> &inputVector );
Run Code Online (Sandbox Code Playgroud)
我在这个块中尝试了多种解决方案:
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.0\vc\include\xmemory0 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
}
Run Code Online (Sandbox Code Playgroud)
我希望这个块从这些线程创建一个结果向量。我究竟做错了什么?
我也看过这里: 如何将期货放入容器中?
我有一个返回“stockDay”类对象向量的函数。向量中的每个元素代表一天的股票数据。
我需要运行这个函数 15 次(在 3 个时期内对 5 只股票)。所以我想线程化该函数,并获取返回的向量。
我可以轻松地为线程创建一个未来、wait() 和 get() 结果向量。但是,我想创建这些期货的向量。如果我理解正确,这将是一个期货向量,每个期货都是一个“stockDay”类对象的向量。
我似乎让那部分工作正常。向量 .size() 显示了正确的元素数量(现在五个用于测试)。
我循环遍历该向量,并对每个元素执行 .wait() 。这也有效。
现在我想 .push_back() 每个未来的 .get() 到另一个向量中。这对我不起作用。
我知道你想让我给你确切的错误信息,但我已经尝试了十几种不同的解决方案,它们都会产生略有不同的错误。
第 174 到 187 行是发生错误的地方。我将它们注释掉,不同的尝试彼此分开。
我理解这里显示的代码: https //solarianprogrammer.com/2012/10/17/cpp-11-async-tutorial/
您将在我的代码第 135 到 148 行中看到该教程中的示例。
如果您尝试push_back()a 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));
}
Run Code Online (Sandbox Code Playgroud)
您还可以push_back()通过使用std::move()以下方法移动它来创建未来的对象:
tasks.push_back(std::move(moveThis));
Run Code Online (Sandbox Code Playgroud)
那么从那个未来得到答案应该很简单:
int temp = tasks[0].get();
Run Code Online (Sandbox Code Playgroud)
注意:一旦你使用了get(),未来的对象就会失效,你不能get()再次使用它。
futureResultsVector.push_back( var ); // Produces Error
你不能复制 a future,你需要移动它:
futureResultsVector.push_back( std::move(var) );
Run Code Online (Sandbox Code Playgroud)
auto tempStuff = var.get(); // Produces Error
该错误意味着它var是一个 const 对象,并且您无法从 const 未来中获得结果。您需要修复代码,使其不会在 const 上下文中访问它。
futureResultsVector.push_back( smaVector42 ); // Produces Error
您正在尝试将向量推入向量。那显然行不通。