最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Is getrandom() in sysrandom.h required to be thread-safe? - Stack Overflow

programmeradmin5浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 5

It 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.

发布评论

评论列表(0)

  1. 暂无评论