Locking around

MRAB google at mrabarnett.plus.com
Wed Aug 6 10:12:15 EDT 2008


On Aug 6, 1:33 pm, Nikolaus Rath <Nikol... at rath.org> wrote:
> Carl Banks <pavlovevide... at gmail.com> writes:
> > Freaky... I just posted nearly this exact solution.
>
> > I have a couple comments.  First, the call to acquire should come
> > before the try block.  If the acquire were to fail, you wouldn't want
> > to release the lock on cleanup.
>
> > Second, you need to change notify() to notifyAll(); notify alone won't
> > cut it.  Consider what happens if you have two threads waiting for
> > keys A and B respectively.  When the thread that has B is done, it
> > releases B and calls notify, but notify happens to wake up the thread
> > waiting on A.  Thus the thread waiting on B is starved.
>
> You're right. Thanks for pointing it out.
>
There's also less chance of deadlock if the files are always locked in
the same order, ie if you sort the files by, say, name, don't lock a
file if one earlier in the sorted list is already locked.



More information about the Python-list mailing list