summaryrefslogtreecommitdiffstats
path: root/Demo/threads/sync.py
diff options
context:
space:
mode:
Diffstat (limited to 'Demo/threads/sync.py')
-rw-r--r--Demo/threads/sync.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Demo/threads/sync.py b/Demo/threads/sync.py
index 6cdf3e8..3044724 100644
--- a/Demo/threads/sync.py
+++ b/Demo/threads/sync.py
@@ -258,6 +258,15 @@
# writing. Then if some other thread is waiting to write, it's
# allowed to proceed. Else all threads (if any) waiting to read are
# allowed to proceed.
+#
+# .write_to_read()
+# Use instead of a .write_in to declare that the thread is done
+# writing but wants to continue reading without other writers
+# intervening. If there are other threads waiting to write, they
+# are allowed to proceed only if the current thread calls
+# .read_out; threads waiting to read are only allowed to proceed
+# if there are are no threads waiting to write. (This is a
+# weakness of the interface!)
import thread
@@ -464,6 +473,18 @@ class mrsw:
self.readOK.broadcast()
self.rwOK.release()
+ def write_to_read(self):
+ self.rwOK.acquire()
+ if not self.writing:
+ raise ValueError, \
+ '.write_to_read() invoked without an active writer'
+ self.writing = 0
+ self.nw = self.nw - 1
+ self.nr = self.nr + 1
+ if not self.nw:
+ self.readOK.broadcast()
+ self.rwOK.release()
+
# The rest of the file is a test case, that runs a number of parallelized
# quicksorts in parallel. If it works, you'll get about 600 lines of
# tracing output, with a line like