2

I have a question about how multithreading and function calls at low-level work. If I call the rand() function from multiple threads, do they access the same function? Where is it stored in the memory? Do all threads access the same memory location for the function? If they all access the same memory location, could it be that two threads are accessing the same function at the same time so one would deadlock or bottleneck/slow down the overall process?

I want to make my threads completely parallel, so I want them to call their own instance of the rand() function. How could I go about doing that?

  • https://stackoverflow.com/questions/6161322/using-stdlibs-rand-from-multiple-threads – pm100 Mar 04 '22 at 00:29
  • There is no such thing as an 'instance of `rand()` function`. There is only one. – user207421 Mar 04 '22 at 00:50
  • You could use one of the functions from the [`drand48()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html) family of functions defined by POSIX, probably`nrand48()`. This can be used even in single-threaded code to provide separate sequences of random numbers to different parts of the same program while still providing the repeatability and isolation of different sequences that is an important part of using separate generators. – Jonathan Leffler Mar 04 '22 at 01:17

2 Answers2

1

The rand function uses static internal state, so if you call it from multiple threads you could get a race condition.

Instead, use the POSIX-defined rand_r() function if your system supports it. It takes the address of a seed value that is updated on each successive call. Then each thread would set its own copy of the seed value to some initial value instead of calling srand.

So instead of this:

srand(INITIAL_VALUE);
val = rand();
val = rand();
val = rand();
...

You would do this:

unsigned int seed = INITIAL_VALUE;
val = rand_r(&seed);
val = rand_r(&seed);
val = rand_r(&seed);
...
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
dbush
  • 205,898
  • 23
  • 218
  • 273
1

I have a question about how multithreading and function calls at low-level work. If I call the rand() function from multiple threads, do they access the same function?

Yes. They all access the rand function.

Where is it stored in the memory?

If by "it", you mean the code for the rand function, then it's stored in memory with the rest of the program's code.

Do all threads access the same memory location for the function? If they all access the same memory location, could it be that two threads are accessing the same function at the same time so one would deadlock or bottleneck/slow down the overall process?

The code is never modified. Two threads accessing data in memory that is never modified do not cause any deadlock, bottleneck, or slow down.

I want to make my threads completely parallel, so I want them to call their own instance of the rand() function. How could I go about doing that?

The instance of the function isn't the issue. The issue is the shared data. Threads accessing shared data will cause performance issues, even if they access the shared data through different functions entirely.

Most likely, you should just not use rand at all and use some code that meets whatever requirements you have. It may be that rand_r does it, but you're probably better off looking for a random-number generator that meets whatever requirements you have.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278