Reference .2.html
Hi,
Looks to me it must be thread-safe, but I could not find any official documentation talks about this.
Is getrandom() in sys/random.h always and necessary thread-safe?
Thanks.
Reference https://man7./linux/man-pages/man2/getrandom.2.html
Hi,
Looks to me it must be thread-safe, but I could not find any official documentation talks about this.
Is getrandom() in sys/random.h always and necessary thread-safe?
Thanks.
Share Improve this question edited Mar 22 at 13:39 Andreas Wenzel 25.8k4 gold badges30 silver badges48 bronze badges asked Mar 22 at 11:56 JS0JS0 674 bronze badges 1- 2 The Linux API is actually in C not C++. If you do use C++ (since you tagged it) - have a look at How do I generate thread-safe uniform random numbers?. – wohlstad Commented Mar 22 at 12:30
1 Answer
Reset to default 5It is generally safe to assume that system calls are thread-safe. See the following Stack Overflow question for further information:
How can I know whether a Linux syscall is thread safe?
The function getrandom
is a system call, because it is in section 2 of the Linux manual pages, which has the title "System Calls Manual".
Even if it were not a system call, but rather a library function, since getrandom
does nothing else than read from /dev/urandom
or /dev/random
, it would probably be safe to assume that it were thread-safe.
Note however that the standard library function rand
(which is not a system call) is not thread-safe, because it reads from and writes to memory that is shared by all threads, and this memory is not protected by a mutex.