Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8486135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:51:31+00:00 2026-06-10T20:51:31+00:00

Is it safe to do this: int fd; void thread_main() { char buf[M]; ssize_t

  • 0

Is it safe to do this:

int fd;

void thread_main()
{
    char buf[M];
    ssize_t r = read(fd, buf, M);
    assert(r == M);
    ...
}

int main()
{
    fd = open("/dev/urandom", O_RDONLY);

    for (int i = 0; i < N; i++)
         start_thread(i);

    for (int i = 0; i < N; i++)
         join_thread(i);
}

That is: After open(2)ing "/dev/urandom" from the main thread, is it safe to read(2) from it unsynchronized from different thread contexts?

Under what circumstances will the assert fire? Will two threads ever get the same data? What could go wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:51:33+00:00Added an answer on June 10, 2026 at 8:51 pm

    Your code is safe insofar as it will not crash. The assert will never trigger. No two threads will (should) ever get the same random data (it is highly unlikely but still possible that two threads get two “different” random sequences which are incidentially the same, so this cannot be 100% guaranteed).

    /dev/urandom will never block or return fewer bytes than you try to read, however, if you read a large enough amount, it will eventually run out of entropy, so the quality of the random numbers will eventually degrade slightly. Usually, this is still just good enough, and it takes a whole while before that happens too, but it’s something to be aware of (most people don’t need to care, but it might not be acceptable, depending on what you do).

    read/write are threadsafe (insofar as they won’t crash or corrupt data or leave descriptors in undefined states) and in this special case also should not intermingle/split up bytes between reads/writes to/from different processes. In general, however, read/write do not guarantee this. They may intermingle data on concurrent reads/writes, on some devices.

    It should however not be a problem, since random bits are still random if you get some other random bits and someone else gets some (different) bits in between.
    If you think it is a problem for you, use readv which guarantees strict atomicity (no mixing/intermingling, ever). Anything that goes in/out of readv/writev is processed as one atomic unit, always (except on pipes, when exceeding a size of PIPE_BUF, as pointed out by rodrigo).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading this: http://en.wikipedia.org/wiki/Thread_safety Is the following function thread-safe? void foo(int y){ int
Is this thread safe? private static bool close_thread_running = false; public static void StartBrowserCleaning()
Is that safe to do something like this: char* charArray = new char[10]; strcat(charArray,
I have a function like this: private void GetRSS(int start, int end) { for
I recently read this article Safe Thread Synchronization as I was curious about the
Why is this java class not Thread safe. class TestClass { private int x;
Is this safe to do? Does fgets terminate the buffer with null or should
This is an authentication script for a website. Is this safe? Is it recent
so... Is this a safe way to use internal links on your site.. By
Given doubles can't be used in volatile fields, is this a safe alternative? Or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.