Standard experimental latch and barrier use ptrdiff_t

独自空忆成欢 提交于 2021-02-05 07:37:51

问题


I was looking at the C++ experimental extensions for concurrency and noticed the new synchronization classes latch, barrier, and flex_barrier. They all implement a standard barrier, either single-use or reusable.

The current documentation states the following signature for their constructors:

explicit latch( ptrdiff_t value );
explicit barrier( std::ptrdiff_t num_threads );
explicit flex_barrier( std::ptrdiff_t num_threads );

With the following explanation for the value or num_threads parameter:

value - the initial value of the internal counter; must be non-negative

num_threads - the number of participating threads for the barrier; must be non-negative

num_threads - the number of participating threads for the flex_barrier; must be non-negative


All three constructors accept a parameter of type std::ptrdiff_t, which is a signed integer type. The documentation then explicitly states that the value must be non-negative.

My question: what is the rationale for choosing the std::ptrdiff_t type as parameter type instead of an unsigned integer type such as std::size_t. It seems to me that using an unsigned integer type is safer as the constructor can then never be called with an invalid parameter value.

I know that the current definition is experimental and inclined to change, but still, the current parameter type was explicitly chosen to be a signed integer type. So there must be some kind of thought behind it, no?


回答1:


This change was made after discussions at the C++ WG21 meeting in Cologne, in February 2015. I wasn't present, but I incorporated the WG's feedback into the document. AFAIK the concern about having an unsigned value was the a negative signed number might accidentally get cast to an unsigned value. As far as using short goes, we wanted to ensure that these concepts were usable in applications written for GPUs where there are potentially very large numbers of subtasks.



来源:https://stackoverflow.com/questions/45857190/standard-experimental-latch-and-barrier-use-ptrdiff-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!