473,799 Members | 3,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7371
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*********@gma il.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*********@gma il.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*********@gma il.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*********@gma il.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
2102
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 for web servers. These apps' re tested with a stress test. That doesn' t work for my module. I searched the web but didn' t find a solution that satisfies me. I think that thread safety errors don' t occur reproduceable and so they' re hard to test and...
4
2794
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 operations?
6
2607
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); ... ... (The program is long so I used a part of it.)
4
2798
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 my language simply propogates up to the rand( ) function from c++. For some reason, C++ will give each thread independent use of their own rand( ) function, so that a rand( ) call from one thread won't affect another thread's call to rand( )....
4
2578
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 as a single application. I was wondering if there is a "Thread-Safety Analysis Wizard"? I'm sure I'm grossly off-base with the following, so I'm prepared to be embarrassed. Please point me in the right direction!
6
3142
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 to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
5
2325
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 the same seed for reproducibility of the results. Now I am porting this (old C89) code and have setup a nice app with threads that drives on one thread the old code and on another the new code, so that I can compare the results and see that nothing...
13
3612
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
4155
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 with xdebug.so. So far as I can tell, the options I used to configure php did not ask for thread-safety, and I also don't see any options to *dis*able thread-safety. Configure --help does show several threading-related options, but none for
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.