# HG changeset patch # Parent 9354df85afd70c3211f8c0037f8087905b55d89d diff --git a/Lib/threading.py b/Lib/threading.py --- a/Lib/threading.py +++ b/Lib/threading.py @@ -287,10 +287,12 @@ def __init__(self, value=1): Semaphore.__init__(self, value) self._initial_value = value + self.TESTWAIT = Event() def release(self): if self._value >= self._initial_value: raise ValueError("Semaphore released too many times") + self.TESTWAIT.wait() return Semaphore.release(self) diff --git a/unbounded.py b/unbounded.py new file mode 100644 --- /dev/null +++ b/unbounded.py @@ -0,0 +1,20 @@ +from threading import BoundedSemaphore, Thread + +bs = BoundedSemaphore(1) + +def runner1(): + bs.acquire() + bs.release() + +def runner2(): + bs.release() + +threads = [Thread(target=runner1), Thread(target=runner2)] +for t in threads: + t.start() + +bs.TESTWAIT.set() +for t in threads: + t.join() + +print("bs._value is {}".format(bs._value))