[New-bugs-announce] [issue17374] Remove restriction against Semaphore having a negative value

Raymond Hettinger report at bugs.python.org
Thu Mar 7 09:57:52 CET 2013


New submission from Raymond Hettinger:

I was working through the problem sets in The Little Book of Semaphores (http://www.greenteapress.com/semaphores/downey08semaphores.pdf) and ran into an issue because Python's threading.Semaphore has an unnecessary restriction against having negative values.

That precludes use cases such as simple barriers (i.e. wait on five signals before a wait is released).

Various descriptions of Semaphores allow their counts to be set to arbitrary integer values.  Here's one definition:

   1. When you create the semaphore, you can initialize its 
      value to any integer, but after that the only operations 
      you are allowed to perform are increment (increase by one) 
      and decrement (decrease by one). You cannot read the current
      value of the semaphore.
   2. When a thread decrements the semaphore, if the result is negative,
      the thread blocks itself and cannot continue until another 
      thread increments the semaphore.
   3. When a thread increments the semaphore, if there are other 
      threads waiting, one of the waiting threads gets unblocked.

The patch is simple, remove the guard in the initialization and change the value==0 test with value<=0 to trigger blocking.

A bit of demonstration code is attached.

----------
components: Library (Lib)
files: barrier.py
messages: 183642
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Remove restriction against Semaphore having a negative value
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file29334/barrier.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17374>
_______________________________________


More information about the New-bugs-announce mailing list