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.