Writing a thread-safe class

Carl Banks pavlovevidence at gmail.com
Fri Sep 11 21:31:11 EDT 2009


On Sep 11, 4:26 pm, Timothy Madden <terminato... at gmail.com> wrote:
> Hello
>
> I would like to write a class with methods that can be accessed by many
> threads at the same time.
>
> For this I have a lock attribute in my class obtained with
> threading.Lock(), in the constructor, and every method begins by
> acquiring the lock and ends by releasing it
>
> My problem is that the lock is still an attribute of the class, and the
> very expression self.lock in the statement self.lock.acquire() is
> performed before locking, thus not being thread-safe.
>
> If I am correct self is a dictionary of object attributes, and if
> another thread has the lock and creates a new attribute, or deletes one,
> in the same time with my lookup for self.lock, than the lookup is
> compromised.

You are not correct.  Dictionary operation (like getting and setting
items) are atomic and limited to one thread at a time, thus thread-
safe.

(In fact, in CPython only one thread can execute Python code at a
time, in most cases.  This is called the Global Interpreter Lock, or
GIL.  If you search this newsgroup for information you will find a LOT
of discission about it.  Other Python implementations such as Jython
may not use the GIL, but I am quite sure dictionary access is thread-
safe on those platforms also.)


> How do people create thread-safe classes in python ?

I think exactly the way you are doing it.  (Some people might use
threading.RLock instead as a minor safety measure, but threading.Lock
will suffice.)


Carl Banks



More information about the Python-list mailing list