简体   繁体   English

c++ 如何从向量中获取 .get() std::future?

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

This is for my c++ class final project.这是我的 C++ 类最终项目。 I am trying to use std::future for the first time, and need some help.我第一次尝试使用 std::future,需要一些帮助。 You can see all of my code here: https://github.com/AdamJHowell/3370-FinalProject你可以在这里看到我所有的代码: https : //github.com/AdamJHowell/3370-FinalProject

Here is the signature of the function that I am trying to get a future from:这是我试图从中获得未来的函数的签名:

        std::vector<stockDay> FuturesSmaEma( std::size_t numP, std::vector<stockDay> &inputVector );

I have tried multiple solutions in this block:我在这个块中尝试了多种解决方案:

        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
        }

I expected this block to create a vector of results from those threads.我希望这个块从这些线程创建一个结果向量。 What am I doing wrong?我做错了什么?

I have also looked here: How do I put futures in a container?我也看过这里: 如何将期货放入容器中?

I have a function that returns a vector of "stockDay" class objects.我有一个返回“stockDay”类对象向量的函数。 Each element in the vector represents the stock data for one day.向量中的每个元素代表一天的股票数据。

I need to run this function 15 times (on five stocks over 3 periods).我需要运行这个函数 15 次(在 3 个时期内对 5 只股票)。 So I want to thread that function, and get the returned vector.所以我想线程化该函数,并获取返回的向量。

I can easily create a single future, wait() for the thread, and get() the resulting vector.我可以轻松地为线程创建一个未来、wait() 和 get() 结果向量。 However, I want to create a vector of those futures.但是,我想创建这些期货的向量。 If I understand this correctly, this will be a vector of futures, each of which will be a vector of "stockDay" class objects.如果我理解正确,这将是一个期货向量,每个期货都是一个“stockDay”类对象的向量。

I seem to have that part working correctly.我似乎让那部分工作正常。 The vector .size() shows the correct number of elements (five right now for testing).向量 .size() 显示了正确的元素数量(现在五个用于测试)。

I loop through that vector, and perform a .wait() on each element.我循环遍历该向量,并对每个元素执行 .wait() 。 That works as well.这也有效。

Now I want to .push_back() each future's .get() into another vector.现在我想 .push_back() 每个未来的 .get() 到另一个向量中。 This is not working for me.这对我不起作用。

I know you want me to give you the exact error message, but I've tried about a dozen different solutions, and they all produce slightly different errors.我知道你想让我给你确切的错误信息,但我已经尝试了十几种不同的解决方案,它们都会产生略有不同的错误。

Lines 174 through 187 is where the errors occur.第 174 到 187 行是发生错误的地方。 I have them commented out and different attempts separated from each other.我将它们注释掉,不同的尝试彼此分开。

I understand the code shown here: https://solarianprogrammer.com/2012/10/17/cpp-11-async-tutorial/我理解这里显示的代码: https : //solarianprogrammer.com/2012/10/17/cpp-11-async-tutorial/

You will see an example from that tutorial in my code on lines 135 to 148.您将在我的代码第 135 到 148 行中看到该教程中的示例。

If you are trying to push_back() a future , that is your error because future has no copy constructor and so an existing one cannot be pushed back.如果您尝试push_back() a future ,那是您的错误,因为future没有复制构造函数,因此无法推回现有的构造函数。

You should instead create a vector<future> container and push_back() std::async operations like so:您应该创建一个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));
}

You could also push_back() a future object by moving it using std::move() :您还可以通过使用std::move()移动它来push_back()未来对象:

tasks.push_back(std::move(moveThis));

Then getting the answer from that future should be as simple as:那么从那个未来得到答案应该很简单:

int temp = tasks[0].get();

Note: Once you use get() , the future object becomes invalid and you cannot get() it a second time.注意:一旦你使用了get() ,未来的对象就会失效,你不能再次get()它。

futureResultsVector.push_back( var ); // Produces Error

You can't copy a future , you need to move it:你不能复制future ,你需要移动它:

futureResultsVector.push_back( std::move(var) );

auto tempStuff = var.get(); // Produces Error

The error means that var is a const object, and you can't get the result out of a const future.该错误意味着var是一个 const 对象,您无法从 const 未来获得结果。 You need to fix the code so it doesn't access it in a const context.您需要修复代码,使其不会在 const 上下文中访问它。

futureResultsVector.push_back( smaVector42 ); // Produces Error

You're trying to push a vector into a vector.您正在尝试将向量推入向量。 That obviously won't work.那显然行不通。

This was due to Microsoft's C++/CLI ( https://en.wikipedia.org/wiki/C%2B%2B/CLI ).这是由于 Microsoft 的 C++/CLI ( https://en.wikipedia.org/wiki/C%2B%2B/CLI )。 Visual Studio 2015 allows for loops like this... Visual Studio 2015 允许这样的循环......

for each( auto var in container )

...but it will not compile with certain types, such as a vector of futures. ...但它不会编译某些类型,例如期货向量。

To put future into the vector, using std::future.push_back(), as:使用 std::future.push_back() 将 future 放入向量中,如:

std::vector<std::future<T>> futures[num_blocks - 1];
futures[i].push_back(SOMETHING_FUTURE);

Or get future from the vector, using (*std::future.begin()).get(), but only once .get() could use.或者从向量中获取未来,使用 (*std::future.begin()).get(),但只有一次 .get() 可以使用。 As:如:

T result =(*futures[i].begin()).get();

May this helps, either maybe perhaps a segmentation fault.这可能会有所帮助,或者可能是分段错误。 Peace!平安!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM