M
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.
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.