[ python-Bugs-1746071 ] class mutex doesn't do anything atomically

SourceForge.net noreply at sourceforge.net
Mon Jul 2 11:23:00 CEST 2007


Bugs item #1746071, was opened at 2007-07-01 10:49
Message generated for change (Comment added) made by dbenbenn
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1746071&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: David Benbennick (dbenbenn)
Assigned to: Nobody/Anonymous (nobody)
Summary: class mutex doesn't do anything atomically

Initial Comment:
    >>> import mutex
    >>> print mutex.mutex.testandset.__doc__
    Atomic test-and-set -- grab the lock if it is not set,
            return True if it succeeded.


The above docstring is wrong: the method is not atomic.  This is easy to see by inspecting the method's code:

    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

Therefore, it is possible for two threads to lock the same mutex simultaneously.  So the mutex module cannot be used for mutual exclusion.

The documentation for mutex says "The mutex module defines a class that allows mutual-exclusion via acquiring and releasing locks."  [http://docs.python.org/lib/module-mutex.html].  Perhaps it would be a good idea to make the module actually do what the documentation says.

----------------------------------------------------------------------

>Comment By: David Benbennick (dbenbenn)
Date: 2007-07-02 05:23

Message:
Logged In: YES 
user_id=95581
Originator: YES

> How are you using mutex with threads, can you please provide some
information.

I'm attaching an example program that demonstrates two threads both
locking the same mutex at the same time.

> If muobj is an instance of mutex class.
> muobj.testandset() for process-a will set the lock.
> muobj.testandset() for process-b will be dealt with self.lock = True
and
> wont be able to set.

That isn't correct.  It is possible for testandset to return True in both
thread-a and thread-b.  What can happen is the following:

1) Thread a calls testandset().  It executes the line "if not
self.locked", and finds the result to be True.
2) The OS switches threads.
3) Thread b calls testandset().  It executes the line "if not
self.locked", and finds the result to be True.
4) Thread b sets "self.locked = 1" and returns True
5) Thread a sets "self.locked = 1" and returns True
File Added: ex.py

----------------------------------------------------------------------

Comment By: O.R.Senthil Kumaran (orsenthil)
Date: 2007-07-01 23:10

Message:
Logged In: YES 
user_id=942711
Originator: NO

Hi David,
I just fired up the docs and found this:
"The mutex module defines a class that allows mutual-exclusion via
acquiring and releasing locks. It does not require (or imply) threading or
multi-tasking, though it could be useful for those purposes."

The docs dont say about threads using mutex object, but instead say if you
want to use threading you can use mutex obj.

How are you using mutex with threads, can you please provide some
information.

If muobj is an instance of mutex class.
muobj.testandset() for process-a will set the lock.
muobj.testandset() for process-b will be dealt with self.lock = True and
wont be able to set.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1746071&group_id=5470


More information about the Python-bugs-list mailing list