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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to