C++17:如何控制执行策略中的线程数?

标签 c++ c++17

C++17 标准引入了一个执行策略参数(例如 std::execution::par_unseq ),它可以传递给 std 中的一些函数。使它们并行执行的库,例如:

std::copy(std::execution::par_unseq, obj1.begin(), obj1.end(), obj2.begin())

在 OpenMP 等其他框架中,可以设置它将使用的最大线程数,例如#pragma omp parallel num_threads(<desired_numer>)在本节中进行本地设置,或 omp_set_num_threads(<desired_number>)将其设置在调用范围内。

我想知道如何在标准 C++ 中实现执行策略。

最佳答案

这是个好问题。也就是说,不幸的是,我认为这是不可能的。 [execpol.general]/1说:

This subclause describes classes that are execution policy types. An object of an execution policy type indicates the kinds of parallelism allowed in the execution of an algorithm and expresses the consequent requirements on the element access functions.

(强调我的)

此外,在那之后,整个[execpol]正在处理 is_execution_policy、(消歧义)策略类型和执行策略对象。

换句话说,执行策略只是以受限的元素访问函数为代价带来了并行的可能性。并没有具体说明这些政策是如何执行的。对我来说,控制并行性的细节似乎更不可能,线程数就是一个例子。

关于C++17:如何控制执行策略中的线程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55941814/

相关文章:

c++ - 比较字符串在 mingw 和 ubuntu gcc 上表现不同

c++ - 未定义的行为和顺序点

c++ - 如何在 C++ 中正确地静态转换 vector ?

c++ - 如果 constexpr 与 sfinae

c++ - 将 std::filesystem 头文件添加到我的程序时编译错误

c++ - 自己的 vector 分配实现

c++ - 在派生类中保留默认构造函数

C++ 避免调用抽象基类的构造函数

c++ - boost 的 shared_ptr(shared_ptr<Y> const & r, T * p) 是做什么用的?

c++ - std::gcd 无法在 g++ 5.4.0 中编译 - 'gcd' 不是 'std' 的成员