Parallel Loops in C++(C++ 中的并行循环)
问题描述
我想知道是否有一种简单直接的方法可以在 C++ 中并行计算诸如 for 和基于范围的 for 循环之类的循环.你会如何实施这样的事情?我从 Scala 知道 map、filter 和 foreach 函数,也许也可以并行执行这些函数?有没有一种简单的方法可以在 C++ 中实现这一点?
I wonder if there is a light, straight forward way to have loops such as for and range based-for loops compute in parallel in C++. How would you implement such a thing? From Scala I know the map, filter and foreach functions and maybe it would also be possible to perform these in parallel? Is there an easy way to achieve this in C++?
我的主要平台是 Linux,但如果它可以跨平台工作就好了.
My primary platform is Linux, but it would be nice if it worked cross-platform.
推荐答案
借助 C++17 中的并行算法,我们现在可以使用:
With the parallel algorithms in C++17 we can now use:
std::vector<std::string> foo;
std::for_each(
std::execution::par_unseq,
foo.begin(),
foo.end(),
[](auto&& item)
{
//do stuff with item
});
并行计算循环.第一个参数指定执行策略
to compute loops in parallel. The first parameter specifies the execution policy
这篇关于C++ 中的并行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 中的并行循环
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
