C++ 在不阻塞的情况下启动剩余期货
我在一个集合上有一个循环,我必须执行一个昂贵的计算。我想使用future类并行地完成这项工作。据我所知,async要么启动线程,要么延迟线程,只有在调用get()或wait()时才启动线程。因此,当我没有启动线程并试图获得结果时,我会阻塞主线程并获得一个顺序处理。是否有方法启动其余的延迟进程,以便并行计算所有进程,并且在调用get()时不会阻塞C++ 在不阻塞的情况下启动剩余期货,c++,multithreading,c++11,future,C++,Multithreading,C++11,Future,我在一个集合上有一个循环,我必须执行一个昂贵的计算。我想使用future类并行地完成这项工作。据我所知,async要么启动线程,要么延迟线程,只有在调用get()或wait()时才启动线程。因此,当我没有启动线程并试图获得结果时,我会阻塞主线程并获得一个顺序处理。是否有方法启动其余的延迟进程,以便并行计算所有进程,并且在调用get()时不会阻塞 //进行计算 矢量未来列表; 用于(自动元素:容器) { push_back(std::async(fct,elem)); } //启动其余进程 //使
//进行计算
矢量未来列表;
用于(自动元素:容器)
{
push_back(std::async(fct,elem));
}
//启动其余进程
//使用结果
用于(自动元素:未来列表)
{
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;
}
#包括
#包括
#包括
#包括
#包括
布尔工作(){
std::this_线程::sleep_for(std::chrono::毫秒(1000));
如果(!(std::rand()%2))抛出std::runtime_错误(“异常”);
返回true;
}
int main(){
常量无符号元素=10;
typedef std::vector future_容器;
未来集装箱期货;
for(无符号i=0;iwait_for(std::chrono::microsides(100))==std::future_status::timeout)++f;
否则{
//注意:由于调用
//线程被抛出这里。
//(见30.6.6未来课程模板)
试一试{
std::cout get()您可以执行以下操作:()
创建此函数:
void processResult_async(std::future<myClass>& f) { processResult(f.get()); }
void processResult_async(std::future&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)));
}
//使用结果
std::矢量结果;
用于(汽车和电子:未来列表)
{
results.push_-back(std::async(std::launch::async,processResult_-async,std::ref(elem));
}
你可以随时轮询未来,看看它是否准备好了?要做到这一点,你可能需要零时间,看看是否有结果准备好了。启动剩余的线程会有什么帮助?你仍然需要等待它们完成。如果你有其他事情可以并行完成,那么在另一个异步
任务中启动它。我有100个计算器因此只有少数直接启动。其余的只在我调用wait()或get()时启动。如果我使用than get(),这将只启动一个任务并阻止主任务。我在该函数中没有其他操作。您缺少std::launch::async
。请尝试将其作为第一个参数传递给std::async
。然后,@juanchopanza是正确的,您仍然必须等待它们完成:)如果无法完成任务,则会引发异常已创建,不是我想要的。@user2988020什么异常?系统错误或errc::资源不可用\u重试_again@user2988020如果您使用的是g++,那么可能必须确保设置了-pthread选项,并且您正在针对pthread进行链接
// 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)));
}