Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc20b76

Browse files
committed
Add comments explaining thread unsafety of this code.
1 parent d03e119 commit cc20b76

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

Lib/whrandom.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
# Adrian Baddeley.
3030

3131

32+
# Multi-threading note: the random number generator used here is not
33+
# thread-safe; it is possible that nearly simultaneous calls in
34+
# different theads return the same random value. To avoid this, you
35+
# have to use a lock around all calls. (I didn't want to slow this
36+
# down in the serial case by using a lock here.)
37+
38+
3239
class whrandom:
3340
#
3441
# Initialize an instance.
@@ -60,13 +67,16 @@ def seed(self, x = 0, y = 0, z = 0):
6067
# Get the next random number in the range [0.0, 1.0).
6168
#
6269
def random(self):
70+
# This part is thread-unsafe:
71+
# BEGIN CRITICAL SECTION
6372
x, y, z = self._seed
6473
#
6574
x = (171 * x) % 30269
6675
y = (172 * y) % 30307
6776
z = (170 * z) % 30323
6877
#
6978
self._seed = x, y, z
79+
# END CRITICAL SECTION
7080
#
7181
return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
7282
#

0 commit comments

Comments
 (0)