question about mutex.py

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

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

Peter Hansen

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.

I think the term "atomic" is meaningful only when the context is known.
For example, "atomic" operations in the Python interpreter are
certainly not atomic within the larger context of the CPU, and atomic
CPU operations are not necessarily atomic in the context of a system
with multiple CPUs. If the context for mutex.py explicitly excludes
multi-threading then you have to interpret "atomic" in whatever context
that defines.

-Peter
 
B

Bengt Richter

I think the term "atomic" is meaningful only when the context is known.
For example, "atomic" operations in the Python interpreter are
certainly not atomic within the larger context of the CPU, and atomic
CPU operations are not necessarily atomic in the context of a system
with multiple CPUs. If the context for mutex.py explicitly excludes
multi-threading then you have to interpret "atomic" in whatever context
that defines.
"Atomic" means trademarked by a company that used to use that name in the 50's
to describe and identify a line toys it put in its breakfast cereal boxes.
The rights are now owned by an IP scavenging company which is trying to sell
them for stock in another IP scavenger with more money left, so be careful.
There's also some related talk of a patent on a method of using semiotic elements
in association with the distribution of digital generic products to make them
distinguishable for any business purpose whatever. IP principles established with
corn flakes and decoder rings are thought to translate perfectly to the digital
domain of FOSS distros including anything toy-like.

Regards,
Bengt Richter
 
P

Peter Hansen

Bengt said:
"Atomic" means trademarked by a company that used to use that name in the 50's
to describe and identify a line toys it put in its breakfast cereal boxes.
The rights are now owned by an IP scavenging company which is trying to sell
them for stock in another IP scavenger with more money left, so be careful.
There's also some related talk of a patent on a method of using semiotic elements
in association with the distribution of digital generic products to make them
distinguishable for any business purpose whatever. IP principles established with
corn flakes and decoder rings are thought to translate perfectly to the digital
domain of FOSS distros including anything toy-like.

And, thus, the origin of an area where I often work with Python:

cereal communications...

(to tastefully blend the "spelling errors" thread with this one ;-) )

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top