c ++如何将.get()std :: future从向量中移除?

时间:2016-04-27 03:54:21

标签: c++ multithreading 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.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
        }

我希望这个块能够从这些线程创建一个结果向量。我做错了什么?

我也看过这里:How do I put futures in a container?

我有一个函数返回“stockDay”类对象的向量。向量中的每个元素代表一天的股票数据。

我需要运行此功能15次(在5个时段的5个股票上)。所以我想对该函数进行线程化,并获取返回的向量。

我可以轻松地为线程创建单个future,wait(),并获得()生成的向量。但是,我想创建一个这些未来的载体。如果我理解正确的话,这将是一个期货的载体,每个期货都是“stockDay”类对象的载体。

我似乎有那部分正常工作。向量.size()显示正确的元素数量(现在有五个用于测试)。

我循环遍历该向量,并对每个元素执行.wait()。这也有效。

现在我希望.push_back()将每个未来的.get()转换为另一个向量。这不适合我。

我知道您希望我向您提供确切的错误消息,但我尝试了十几种不同的解决方案,并且它们都会产生稍微不同的错误。

第174到187行是发生错误的地方。我让他们评论出来,不同的尝试相互分离。

我理解此处显示的代码:https://solarianprogrammer.com/2012/10/17/cpp-11-async-tutorial/

您将在第135至148行的代码中看到该教程的示例。

4 个答案:

答案 0 :(得分:3)

如果您尝试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));
}

您还可以使用push_back()

移动未来对象std::move()
tasks.push_back(std::move(moveThis));

然后从那个未来得到答案应该简单:

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

注意:使用get()后,未来的对象将变为无效,您不能再次get()

答案 1 :(得分:3)

  

futureResultsVector.push_back( var ); // Produces Error

你不能复制future,你需要移动它:

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

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

错误意味着var是一个const对象,你无法从const future中获得结果。您需要修复代码,以便它不会在const上下文中访问它。

  

futureResultsVector.push_back( smaVector42 ); // Produces Error

您正在尝试将矢量推入矢量。这显然是行不通的。

答案 2 :(得分:0)

这是由于Microsoft的C ++ / CLI(https://en.wikipedia.org/wiki/C%2B%2B/CLI)。 Visual Studio 2015允许这样的循环......

for each( auto var in container )

...但它不会使用某些类型进行编译,例如期货矢量。

答案 3 :(得分:0)

使用std :: future.push_back()将future放入向量中,如下所示:

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();

这可能会有所帮助,也许是分段错误。和平!