简体   繁体   中英

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

This is for my c++ class final project. I am trying to use std::future for the first time, and need some help. You can see all of my code here: 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. 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). 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. 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.

I seem to have that part working correctly. The vector .size() shows the correct number of elements (five right now for testing).

I loop through that vector, and perform a .wait() on each element. That works as well.

Now I want to .push_back() each future's .get() into another vector. 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. 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/

You will see an example from that tutorial in my code on lines 135 to 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.

You should instead create a vector<future> container and push_back() std::async operations like so:

#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() :

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.

futureResultsVector.push_back( var ); // Produces Error

You can't copy a future , you need to move it:

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. You need to fix the code so it doesn't access it in a const context.

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 ). Visual Studio 2015 allows for loops like this...

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::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. As:

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

May this helps, either maybe perhaps a segmentation fault. Peace!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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