简体   繁体   English

为什么并行std :: for_each返回void

[英]Why parallel std::for_each return void

I am reading C++17 in detail . 我正在详细阅读C ++ 17 There is a description for std::for_each below, in this book: 本书下面对std :: for_each进行了说明:

In the serial version of for_each, the version that was available before C++17 >you get a unary function as a return value from the algorithm. 在for_each的串行版本中,该版本在C ++ 17之前可用,您会从算法中获得一元函数作为返回值。 Returning such an object is not possible in a parallel version, as the order >of invocations is indeterminate. 在并行版本中,返回这样的对象是不可能的,因为调用的顺序是不确定的。

I still understand, why parallel std::for_each can not return the functor f . 我仍然明白,为什么并行的std :: for_each无法返回函子f Although the order of invocations is indeterminate, parallel for_each block to wait invoke finish.When every parallel operation finish, returning the f is seemed ok. 尽管调用的顺序不确定,但并行的for_each块等待调用完成。当每个并行操作完成时,返回f似乎没问题。

The idea behind returning the functor is to allow the functor to accumulate state across successive calls. 返回函子的想法是允许函子在连续的调用中累积状态。 The accumulated state could be accessed through the returned copy. 可以通过返回的副本访问累积状态。

Accumulation requires serial access to the accumulated state, which would defeat the purpose of parallel execution. 累加需要对累加状态的串行访问,这会破坏并行执行的目的。 Essentially, each thread of execution will get a copy of the function object and their state is not shared. 本质上,每个执行线程都将获得功能对象的副本,并且它们的状态不会共享。

The standard has following note: 该标准有以下注释:

[Note: Does not return a copy of its Function parameter, since parallelization may not permit efficient state accumulation. [注意:不返回其Function参数的副本,因为并行化可能不允许有效的状态累积。 — end note] —尾注]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM