[Python-ideas] Fwd: Concurrent safety?

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Nov 3 22:35:47 CET 2011


Mike Meyer wrote:

> This case would still throw an exception, because what needs to be
> locked isn't balance, but whatever balance is an attribute of.

All right, suppose we have an "account" object that
holds the balance. Now we can do

    locking account:
       account.balance = account.balance + deposit

That's okay. But what if there are two accounts
involved, and we want to do

    account1.balance = account1.balance - amount
    account2.balance = account2.balance + amount

These two operations need to be done atomically,
but there is no single object to lock, so we have
to lock both of them -- and then there is an
opportunity for deadlock if two threads do this
in different orders.

> and there's a further requirement that you can only nest
> locking statements if the inner one locks a subset of the outer one.

Then it's *impossible* to solve the above problem,
because neither of the objects needing to be locked
is contained in the other.

The notion of containment is not even well defined
in general, because Python objects can form arbitrary
graphs.

-- 
Greg



More information about the Python-ideas mailing list