How to go about. On read/write locks

Carl Banks pavlovevidence at
Tue Apr 7 04:09:02 CEST 2009

On Apr 6, 2:23 pm, "Diez B. Roggisch" <de... at> wrote:
> > This is a classical synchronization problem with a classical solution:
> > You treat the readers as a group, and the writers individually. So you
> > have a write lock that each writer has to acquire and release, but it is
> > acquired only by the first reader and released by the last one.
> > Therefore you need a counter of the number of readers, and manipulations
> > of this counter must be protected by another lock.
> I was going to suggest a similar approach but refused to because of a
> problem I see with your code as well - if the readers are reading very
> fast (the OP didn't state what his actual application is, so it might
> not be a consumer-producer scheme which I don't think a dict would be
> the natural choice anyway) they can block the writer from writing
> alltogether.

You could implement some kind of fair ordering where whoever requests
a lock first is guaranteed to get it first, but I can't think of a way
to do that without requiring all readers to acquire two locks.

Carl Banks

More information about the Python-list mailing list