Writing a thread-safe class
Timothy Madden
terminatorul at gmail.com
Fri Sep 11 19:26:34 EDT 2009
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.
How do people create thread-safe classes in python ?
Thank you,
Timothy Madden
More information about the Python-list
mailing list