473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

rand() and thread safety

I hear rand() is not thread safe. I was using it, foolish man that I
am. But what is meant exactly by unsafe? What can happen? Bizzare
results from rand()? Something worse?

Thanks,
Eyal.

Jul 23 '05 #1
7 7327
Oh, just to make things clear, I wasn't reading the return value from
rand() in two threads or something. I was just calling it in one
thread, locally, and using it just there. Could that still pose a
problem?!

Thanks

Jul 23 '05 #2
ey*********@gmail.com wrote:
I hear rand() is not thread safe. I was using it, foolish man that I
am. But what is meant exactly by unsafe? What can happen? Bizzare
results from rand()? Something worse?


Ask whoever said it.

Generally speaking, neither the C nor the C++ standard imposes any
requirements on code used in multi-threaded applications, so it's
vacuously true that nothing in either language is thread safe. More
usefully, though, compiler writers and library writers generally know
that people sometimes write multi-threaded applications (even when they
shouldn't, but that's a separate discussion), and take reasonable care
to ensure that their code works "correctly" in multiple threads. Ask
your vendor what "correctly" means.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3
Wait, are you saying that on some platforms (gnu mips compiler, for
example), behaviour could be truly and really undefined? As in a call
to rand() corrupting memory due to some unfortunate thread timing?
Even if the rand() is used locally in a thread (return value not shared
among threads)?! I will be shocked out of my shoes if you say yes! ;-)

Thanks

Jul 23 '05 #4

Pete Becker wrote:

ey*********@gmail.com wrote:
I hear rand() is not thread safe. I was using it, foolish man that I
am. But what is meant exactly by unsafe? What can happen? Bizzare
results from rand()? Something worse?


Ask whoever said it.

Generally speaking, neither the C nor the C++ standard imposes any
requirements on code used in multi-threaded applications, so it's
vacuously true that nothing in either language is thread safe. More
usefully, though, compiler writers and library writers generally know
that people sometimes write multi-threaded applications (even when they
shouldn't, but that's a separate discussion), and take reasonable care
to ensure that their code works "correctly" in multiple threads. Ask
your vendor what "correctly" means.


POSIX defines "Reentrant Function": "A function whose effect, when
called by two or more threads, is guaranteed to be as if the threads
each executed the function one after another in an undefined order,
even if the actual execution is interleaved" (just like everything,
it holds as long as application doesn't trigger undefined behavior
[for example by using pointer arguments which lead to violation of
XBD 4.10]). With respect to rand(), it says "The rand() function need
not be reentrant. A function that is not required to be reentrant is
not required to be thread-safe.". Under TSF option, POSIX provides
"thread-safe" rand_r() function.

regards,
alexander.
Jul 23 '05 #5
ey*********@gmail.com wrote:
Wait, are you saying that on some platforms (gnu mips compiler, for
example), behaviour could be truly and really undefined?


Ask the GNU folks.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6
OK, thanks everyone! :-)

Jul 23 '05 #7
ey*********@gmail.com wrote:
Oh, just to make things clear, I wasn't reading the return value from
rand() in two threads or something. I was just calling it in one
thread, locally, and using it just there. Could that still pose a
problem?!

No problem at all.

Also a compiler that supports multithreading, usually provides a thread-safe
implementation of the standard library. Otherwise it will state in its documentation if
something is not thread safe.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Alexander Fleck | last post by:
Hi, I' ve to make a software module thread safe. I know how to realize that and what' re the main topics of thread safety. But I don' t know how thread safety can be tested. I read about a test...
4
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read...
6
by: Roka | last post by:
Hi all. I'm reading a program which used the sentence below: #define NUM_THREADS 10 ... ... int rand_num; rand_num = 1+ (int) (9.0 * rand() / (RAND_MAX + 1.0)); sleep(rand_num); ... ......
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
5
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
0
by: Graham Wideman | last post by:
Folks: Can anyone tell me what controls php's "thread safety" feature? I have an installation where phpinfo() is showing Thread safety: enabled, whereas I need it disabled in order to work...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.