summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-05-29 17:51:59 (GMT)
committerGuido van Rossum <guido@python.org>1998-05-29 17:51:59 (GMT)
commitcc20b76ad00d5932eda124a49818c0af4265d3d4 (patch)
tree2c0112a1d92d1fdfced6bb0fb66b9c418622c4d5
parentd03e1197cb5052e3f758794e2a7aecf9f5ca5f46 (diff)
downloadcpython-cc20b76ad00d5932eda124a49818c0af4265d3d4.zip
cpython-cc20b76ad00d5932eda124a49818c0af4265d3d4.tar.gz
cpython-cc20b76ad00d5932eda124a49818c0af4265d3d4.tar.bz2
Add comments explaining thread unsafety of this code.
-rw-r--r--Lib/whrandom.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/whrandom.py b/Lib/whrandom.py
index 95e88f3..7f33fd6 100644
--- a/Lib/whrandom.py
+++ b/Lib/whrandom.py
@@ -29,6 +29,13 @@
# Adrian Baddeley.
+# Multi-threading note: the random number generator used here is not
+# thread-safe; it is possible that nearly simultaneous calls in
+# different theads return the same random value. To avoid this, you
+# have to use a lock around all calls. (I didn't want to slow this
+# down in the serial case by using a lock here.)
+
+
class whrandom:
#
# Initialize an instance.
@@ -60,6 +67,8 @@ class whrandom:
# Get the next random number in the range [0.0, 1.0).
#
def random(self):
+ # This part is thread-unsafe:
+ # BEGIN CRITICAL SECTION
x, y, z = self._seed
#
x = (171 * x) % 30269
@@ -67,6 +76,7 @@ class whrandom:
z = (170 * z) % 30323
#
self._seed = x, y, z
+ # END CRITICAL SECTION
#
return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
#