This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

drand48() (and erand48) returns only zeros and pthread application problems - in cygwin 1.7.8 returns always same values


About all the problems with drand48, explained in:
   * http://www.cygwin.com/ml/cygwin/2010-12/msg00460.html

Christopher Faylor fixed it in 20101229 snapshot:
http://www.cygwin.com/snapshots/winsup-changelog-20101226-20101229

I have realized that the fix does not work as expected, it is used
thread memory address to initialize srand48 with srand48 ((long int)
&x);

--- winsup-src-20101226/cygwin-snapshot-20101226-1/winsup/cygwin/cygtls.cc	2010-02-28
15:55:33.000000000 +0000
+++ winsup-src-20101229/cygwin-snapshot-20101229-1/winsup/cygwin/cygtls.cc	2010-12-29
06:34:24.000000000 +0000
@@ -94,4 +95,5 @@ _cygtls::init_thread (void *x, DWORD (*f
       local_clib._current_locale = "C";
       locals.process_logmask = LOG_UPTO (LOG_DEBUG);
+      srand48 ((long int) &x);
     }

Now, drand48 doesnot returns zero in thread, but the problem is that
in every execution the adress should be the same, because it returns
always the same values.
The test code can be found in:
http://www.cygwin.com/ml/cygwin/2010-12/msg00459.html

$ uname -a
CYGWIN_NT-5.1 ES-H0TJT2J 1.7.8s(0.234/5/3) 20101229 01:34:45 i686 Cygwin

TEST 1  => seed=2 ; threads=2
$ ./drand48threadtest 2 2
Init srand48 with: 2
Start main
Main: Creating thread #0 - drand48: 0.912433
        Executing thread #0.0 - drand48: 0.985887
Main: Creating thread #1 - drand48: 0.159083
        Executing thread #0.1 - drand48: 0.212671
        Executing thread #1.0 - drand48: 0.789110
End main
        Executing thread #0.2 - drand48: 0.460958
        Executing thread #1.1 - drand48: 0.513941
        Executing thread #1.2 - drand48: 0.049337

TEST 2 => seed=3; threads=2
$ ./drand48threadtest 3 2
Init srand48 with: 3
Start main
Main: Creating thread #0 - drand48: 0.783235
        Executing thread #0.0 - drand48: 0.985887
Main: Creating thread #1 - drand48: 0.863673
        Executing thread #0.1 - drand48: 0.212671
        Executing thread #1.0 - drand48: 0.789110
End main
        Executing thread #0.2 - drand48: 0.460958
        Executing thread #1.1 - drand48: 0.513941
        Executing thread #1.2 - drand48: 0.049337

TEST 3=> seed=2 threads=1
$ ./drand48threadtest 2 1
Init srand48 with: 2
Start main
Main: Creating thread #0 - drand48: 0.912433
        Executing thread #0.0 - drand48: 0.985887
        Executing thread #0.1 - drand48: 0.212671
End main
        Executing thread #0.2 - drand48: 0.460958

Main thread generated numbers depends on seed, that is specified in
first parameter, but in other threads it generates same numbers in
diferent executions because thread seed is calculated from memory
address:
        Executing thread #0.0 - drand48: 0.985887
        Executing thread #0.1 - drand48: 0.212671
        Executing thread #1.0 - drand48: 0.789110
        Executing thread #1.1 - drand48: 0.513941

The problem root was on newlib implementation, but I think we can make
a easy workaround changing srand48 line:

-      srand48 ((long int) &x);
+     srand48(random());

Is it possible to call random() in that point?

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]