question about mutex.py

2006-01-06 Thread m_palmer45
Hi, I was looking at the code in the standard lib's mutex.py, which is
used for queuing function calls. Here is how it lets you acquire a
lock:


def testandset(self):
"""Atomic test-and-set -- grab the lock if it is not set,
return True if it succeeded."""
if not self.locked:
self.locked = 1
return True
else:
return False

Question: Is that really atomic? With multiple threads, couldn't the
state of self.locked change between the 'if' clause and the
'self.locked=1' ?
If so, wouldn't a possible fix be to delete and set an 'unlocked' flag:

   def testandset(self):
   try:
del self.unlocked# this should be atomic, shouldn't it
return True
   except AttributeError:
return False

? Any advice welcome. M.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about mutex.py

2006-01-06 Thread m_palmer45
yes, I read it, and I even know about threading's existence. I just
thought that if something claims to be atomic, it better should be.

-- 
http://mail.python.org/mailman/listinfo/python-list