Locking and try/finally

Jeremy Yallop jeremy at jdyallop.freeserve.co.uk
Wed Dec 4 09:19:10 EST 2002


Daniel Dittmar wrote:
> Duncan Grisby wrote:
>> One way to make this kind of thing cleaner is to make locking part of
>> the language syntax.

(like Java's `synchronized' statement).

>> Modula-3, one of the inspirations of Python, did
>> things like this... (the syntax is probably wrong, but you get the
>> idea)
>>
>>   PROCEDURE critical_function(self: T) =
>>     BEGIN
>>       LOCK self.mu DO
>>         (* Manipulate data... *)
>>       END;
>>     END;
>>
>> The compiler turned such statements into the equivalent acquire /
>> release pair using TRY..FINALLY.
> 
> If Python had Smalltalk like blocks instead of list comprehension, then one
> could write
> 
> self.mutex.do ([ ... code running in critical section ...])

\begin{thread-convergence}

If Python were Common Lisp (with macros and unwind-protect) then one
could write:

   (with-lock-held (lock)
      (fancy-stuff)
      (more-fancy-stuff))

\end{thread-convergence}

Jeremy.



More information about the Python-list mailing list