[Python-Dev] New PEP: 319

Greg Ewing greg@cosc.canterbury.ac.nz
Tue, 17 Jun 2003 11:44:55 +1200 (NZST)


Roman Suzi <rnd@onego.ru>:

> I have not thought about locking objects, but locking places of a
> program from multiple entry.

If we're to have implicit lock objects, this is perhaps the
right way to think about it. But the locking needs to apply
to more than just one code block. Consider

  def increment(self):
    synchronize:
      self.count += 1

  def decrement(self):
    synchronize:
      self.count -= 1

If the two statements are synchronized independently, this
will not work.

I think the right level to synchronize at is a whole class:

  synchronized class Counter:

    def __init__(self):
      self.counter = 0

    def increment(self):
      self.count += 1

    def decrement(self):
      self.count -= 1

the semantics being that each instance of Counter gets
its own lock object, and the lock is acquired before any
method in it is entered and released when it is exited.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+