如何将期货放入集装箱? [英] How do I put futures in a container?

查看:70
本文介绍了如何将期货放入集装箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将异步生成的期货放入向量中,所以我不必执行以下操作:

I'm trying to put the futures generated by async in a vector, so I don't have to do something like:

auto f1 = async(....);
auto f2 = async(....);
...
f1.get();
f2.get();
...

此代码收到的编译错误是调用已删除 std :: _ 1 :: future的构造函数。谁能帮我正确地做到这一点。

The compilation error I am receiving with this code is "Call to deleted constructor of 'std::_1::future". Can anyone help me with how to do this properly. Not sure about copying the future's into the vector either.

void AudioAnalyzer::retrieve()
{
    deque<shared_ptr<AudioAnalysis>>tempData(data);
    vector<future<void>> futures;
    for (int i = 0; i < NUM_THREADS; ++i)
    {
        auto f = async(bind(&AudioAnalyzer::analysisThread, this, _1), ref(tempData));
        futures.push_back(f);
    }

    for (auto& f : futures)
    {
        f.get();
    }

}

void AudioAnalyzer::analysisThread(deque<shared_ptr<AudioAnalysis>>& aq )
{

    while (true)
    {
        m.lock();
        if (aq.size() == 0)
        {
            m.unlock();
            break;
        }
        auto aa = aq.front();
        aq.pop_front();
        m.unlock();

        if (false) //testing
        {
            retrieveFromDb(aa);
        }
        else
        {
            analyzeAudio(aa);
        }
    }
}


推荐答案

未来是不可复制的,但是它们是可移动复制的。您需要将它们移入容器:

Futures are not copyable, but they are move-copyable. You need to move them into the container:

futures.push_back(std::move(f));

此处, std :: move(f)看起来像一个右值,导致正确的 std :: vector< future< void> ;> :: push_back 重载。

Here, std::move(f) looks like an rvalue, resulting in the right std::vector<future<void>>::push_back overload being selected.

这篇关于如何将期货放入集装箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆