c ++如何使我.get() std::future out of a vector.

c++ How do I .get() std::future out of a vector?

本文关键字:future out of vector std 何使我 get      更新时间:2023-10-16

这是我的 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();

这可能有所帮助,或者可能是分段错误。和平!