开始剩余的期货而不阻塞

Start remaining futures without blocking

本文关键字:开始      更新时间:2023-10-16

我在一个集合上有一个循环,在那里我必须执行昂贵的计算。我想使用未来的类并行地执行此操作。据我所知,async要么启动线程,要么推迟线程,只在我调用get()或wait()时启动线程。因此,当我有线程没有启动并试图获得结果时,我会阻塞主线程并获得顺序处理。有没有一种方法可以启动剩余的延迟进程,这样所有的东西都是并行计算的,当我调用get()时不会阻塞。

// do the calculations
std::vector<std::future<class>> futureList;
for (auto elem : container)
{
  futureList.push_back(std::async(fct, elem));
}
// start remaining processes
// use the results
for (auto elem : futureList)
{
  processResult(elem.get())
} 

谢谢你的帮助。

您可以使用:

std::async(std::launch::async, fct, elem)

样品:

#include <iostream>
#include <future>
#include <chrono>
#include <vector>
#include <stdexcept>
bool work() {
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    if( ! (std::rand() % 2)) throw std::runtime_error("Exception");
    return true;
}
int main() {
    const unsigned Elements = 10;
    typedef std::vector<std::future<bool>> future_container;
    future_container futures;
    for(unsigned i = 0; i < Elements; ++i)
    {
        futures.push_back(std::async(std::launch::async, work));
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    while( ! futures.empty()) {
        future_container::iterator f = futures.begin();
        while(f != futures.end())
        {
            if(f->wait_for(std::chrono::milliseconds(100)) == std::future_status::timeout) ++f;
            else {
                // Note:: Exception resulting due to the invokation of 
                //        the thread are thrown here.
                //        (See 30.6.6 Class template future)
                try {
                    std::cout << f->get() << 'n';
                }
                catch(const std::exception& e) {
                    std::cout << e.what() << 'n';
                }
                f = futures.erase(f);
            }
        }
    }
    return 0;
}

您可以执行以下操作:(http://coliru.stacked-crooked.com/a/005c7d2345ad791c)

创建此功能:

void processResult_async(std::future<myClass>& f) { processResult(f.get()); }

然后

// use the results
std::vector<std::future<void>> results;
for (auto& elem : futureList)
{
    results.push_back(std::async(std::launch::async, processResult_async, std::ref(elem)));
}