for循环/ for_each的每个迭代可以并行完成吗? (C ++ 11) [英] Can each Iteration of a for loop/for_each be done in parallel? (C++11)

查看:597
本文介绍了for循环/ for_each的每个迭代可以并行完成吗? (C ++ 11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迭代结构的向量,并单独处理每个结构。

它看起来像这样:

I'm iterating over a vector of structs and processing each struct individually.
It looks something like this:

for_each(begin(data),end(data),DoTask);
//assume "data" is std::vector<DataT>  
//assume DoTask is a function that takes a DataT by reference

因为DoTask连接到特定的网站和分析HTML。

什么是最好的方式来加快这个速度?

我的目标是同时分析多个DataTs。 >
我是线程的新用户,但 std :: async std :: future 看起来很有前途。

The code is significantly slow because DoTask connects to particular websites and analyzes HTML.
What would be the best way to speed this up?
My goal is to analyze multiple DataTs at the same time.
I'm very new to threading, but std::async and std::future look promising.

推荐答案

您可以这样做

for(T& d : data) std::thread(DoTask, d).detach();

或者你可以使用更复杂的东西,比如Intel的Thread Building Blocks和 parallel_for (不是名称?)函数。

Or you can use something more complicated like Intel's Thread Building Blocks and the parallel_for (isn't that the name?) function thereof.

这篇关于for循环/ for_each的每个迭代可以并行完成吗? (C ++ 11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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