Andrey Tarasevich <
andreyta...@hotmail.com> wrote:
> So, if it does use a shared resource, does the standard give us a
> guarantee that polling automatic (as in the example above) or
> `thread_local` objects of `std::random_device` type is thread-safe?
I somehow have the impression that the principle is that when the programmer
is not given a choice about whether something is shared or not, it will be
guaranteed to be thread-safe or thread-local (if it is indeed shared).
For example you have no choice about the internal implementation details
of std::fopen(), therefore it has to be thread-safe. errno is guaranteed
to be thread-local (again, because you have no choice over where it's
located). Rather obviously 'new' (and 'malloc()') has to be thread-safe
because you don't have a "local new" choice. (And this is one of the reasons
why dynamic memory allocation is so slow, even in single-threaded programs.)
A function-local static is guaranteed to be thread-safe.
Thus it would make sense that if the programmer has no choice over whether
std::random_device uses some shared resource nor not, it has to be thread-safe
if it doesn't.
(Of course there's at least one argument against the above: The programmer
has no choice about the internal details of std::cout, yet it's not
guaranteed to be thread-safe. Or, more precisely, the order in which
different characters are outputted is not guaranteed if more than one
thread uses it at the same time. I assume it's at least guaranteed that
std::cout will not outright malfunction and crash the program even if
it's used simultaneously by multiple threads...)