Re: Scoped Lock

From: Daniel Dittmar (daniel.dittmar_at_sap.com)
Date: 01/05/04


Date: Mon, 5 Jan 2004 13:40:01 +0100

Marco Bubke wrote:
> This does not look nice to me. There should be something me easily.
> Maybe that:
>
> def do_something() lock(mutex):
> #this stuff is locked by the mutex
>
> So you don't forget to release the lock.

You could create a class that locks the mutex in the constructor and unlocks
it in the __del__ method.

class ScopedLock:
    def __init__ (self, mutex):
        self.mutex = mutex
        mutex.acquire()

    def __del__ (self):
        self.mutex.release ()

use as
def do_something():
    lock = ScopedLock (mutex)
    # do something
    # lock will be automatically released when returning

This works only in the current implementation of CPython where local
variables are usually (*) deleted when they fall out of scope.

(*) usually: unless they are returned or added to another object

And I guess with metaclasses, you can achieve something like Java's
synchronized methods.

Daniel



Relevant Pages

  • Re: Recursive mutex that can be waited upon (pthread)
    ... While you can use a mutex to avoid that data is changed, for me having a mutex does not mean that data is not changed, it only means that data is not changed by a different thread. ... My own thread may of course change the data, hence functions I call may want to change the data and if they do so, they must be sure that these changes are atomically, hence they must lock the object and they simply can't rely that I locked the object before -> thus I need recursive locks. ... Then I could as well throw out threads of my code and just use a single thread going through an event queue. ... you have a predicate condition on an invariant. ...
    (comp.programming.threads)
  • [patch 6/8] mutex subsystem, core
    ... mutex implementation, core files: just the basic subsystem, no users of it. ... straightforward mutexes with strict semantics: ... + * Block on a lock - add ourselves to the list of waiters. ...
    (Linux-Kernel)
  • [patch 6/8] mutex subsystem, core
    ... mutex implementation, core files: just the basic subsystem, no users of it. ... straightforward mutexes with strict semantics: ... + * Block on a lock - add ourselves to the list of waiters. ...
    (Linux-Kernel)
  • [PATCH 4/8] adaptive real-time lock support
    ... The Real Time patches to the Linux kernel converts the architecture ... compromising the integrity of critical sections protected by the lock. ... while retaining both the priority inheritance protocol as well as the ... the RT Mutex has been ...
    (Linux-Kernel)
  • Re: WaitForSingleObject() will not deadlock
    ... You keep talking about needing to know the reference count. ... algorithm that uses a recursive mutex that cares in the slightest what the reference count ... attempts to lock appears to be isomorphic to a recursive lock, ... cycle detection until you reach the end of the list, ...
    (microsoft.public.vc.mfc)