-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
math/rand: document that math.Rand is not safe for concurrent use #3611
Comments
In general the rule is this: top-level functions like strings.Split or fmt.Printf or rand.Int63 may be called from any goroutine at any time (otherwise programming with them would be too restrictive), but objects you create (like a new bytes.Buffer or rand.Rand) must only be used by one goroutine at a time unless otherwise noted (like net.Conn's documentation does). There are not enough stack frames to say for sure, but it sounds like you are calling Int63 on your own allocated rand.Rand from multiple goroutines. That is not promised to work, and it turns out does not. If you are actually calling the top-level function rand.Int63 and it is crashing, then that's a bug on our part and we should investigate further. Please let us know which it is. Thanks. Status changed to WaitingForReply. |
I'm calling rand.Intn(int) from a rand that I created. Is the best practice to use a mutex to control access to the rand I create? Additionally, couldn't the code in rng.go be made goroutine-safe (preventing tap and feed from jumping off the end of the vector) by doing the decrement and range fix against a local variable? tap := rng.tap -1 if tap < 0 { tap += _LEN } rng.tap = tap feed := rng.feed -1 if feed < 0 { feed += _LEN } rng.feed = feed Thanks. |
: I'm calling rand.Intn(int) from a rand that I created. Is the best practice : to use a mutex to control access to the rand I create? Yes, using a mutex is the right solution, or call the package's top-level Intn function. : Additionally, couldn't the code in rng.go be made goroutine-safe (preventing : tap and feed from jumping off the end of the vector) by doing the decrement : and range fix against a local variable? The rewrite would probably work, but you still have the problem that simultaneous calls to Intn are returning the same value. Also, an optimizing compiler would be allowed to rewrite your new code into the old code. Russ |
OK thanks. I'm creating different rands in order to reduce contention for the top-level rand. Please consider a revision along the lines I suggested. I'd think different rands getting the same value occasionally would be preferable to an out-of-range exception - at least where the compiler doesn't optimize out the temp variables. |
smallnest
added a commit
to smallnest/C1000K-Servers
that referenced
this issue
Jul 15, 2015
tsenart
pushed a commit
to d2iq-archive/mesos-dns
that referenced
this issue
Oct 8, 2015
Only the global package level rand.Source is safe for concurrent use. Instantiated ones need their own synchronization mechanisms. See: golang/go#3611
tsenart
pushed a commit
to d2iq-archive/mesos-dns
that referenced
this issue
Oct 8, 2015
Only the global package level rand.Source is safe for concurrent use. Instantiated ones need their own synchronization mechanisms. See: golang/go#3611
jkodumal
added a commit
to launchdarkly/go-client
that referenced
this issue
May 14, 2016
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by gar3ts:
The text was updated successfully, but these errors were encountered: