srandom thread-safety

This is a discussion on srandom thread-safety within the C++ Programming forums, part of the General Programming Boards category; I have found online evidence that suggest that srandom is not thread safe. I am unable to proove this because ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    srandom thread-safety

    I have found online evidence that suggest that srandom is not thread safe. I am unable to proove this because I don't know if this was fixed in recent years. Do you know whether this thread safety issue still exists? If this is true, what would be a work around for srandom.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    A simple workaround would be to simply not use srand()/rand() and use some other random number generator library. I believe that implementation of the Mersenne Twister are a popular choice, and may be easier/better to use than most implementations of the standard random number generator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by chrismiceli View Post
    I have found online evidence that suggest that srandom is not thread safe. I am unable to proove this because I don't know if this was fixed in recent years. Do you know whether this thread safety issue still exists? If this is true, what would be a work around for srandom.
    Why would thread safety matter in a function you only call once?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Josuttis claims in "The C++ Standard Library: A Tutorial and Reference" (section 9.8.4) that:
    Old global C functions such as rand() store their local state in a static variable. However, this has some disadvantages: For example, the random number generator is inherently thread unsafe, and you can't have two independent streams of random numbers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    This page says that srandom() is not thread-safe: http://www.ncsa.uiuc.edu/UserInfo/Re..._quick_ref.htm

    This page suggests that srandom_r() is an equivalent thread-safe function: http://www.llnl.gov/asci/platforms/white/code_dev/

    I know that Cygwin has certain _r functions, perhaps your compiler does too.

    But why should srandom() be thread-safe? It initializes the random number generator. Surely you can figure out a way to do that in one thread only.

    [edit] Three posts while I was typing! Wow . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I am not using it, this library is.
    http://www.ossp.org/pkg/lib/uuid/
    I can modify the library, but the solution must be extremely portable.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    srandom() is "extremely portable"?

    Since you usually only call srandom() once, you should be able to work around its thread-unsafeness, or you could probably make a thread-safe wrapper for it if you felt like it.

    I can't open that archive on this computer (nor can I install any archive programs here!), so I can't look at the source. Can you post the code that uses srandom()? Are you calling srandom() more than once? (Note: I'm just saying "you" out of habit.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Prelude's Mersenne Twister implementation found on eternallyconfuzzled.com should be pretty portable. I have a C++ port of that, and there are also Boost's random number generators.

    EDIT:
    Oh wait, the uploaded implementation file of my C++ version of Prelude's implementation has a bug with the seed generation function.
    Last edited by laserlight; 06-27-2007 at 01:10 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 08:27 AM
  2. thread safety using Interlocked
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-16-2008, 04:07 AM
  3. thread safety in Windows Service design
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-14-2008, 07:25 AM
  4. Thread safety in WinAPI
    By Magos in forum Windows Programming
    Replies: 5
    Last Post: 05-14-2006, 06:55 PM
  5. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21