A lock that times out but doesn't poll

Antoon Pardon apardon at forel.vub.ac.be
Fri Nov 26 04:21:04 EST 2004


Op 2004-11-25, Jive schreef <someone at microsoft.com>:
> Okay, I've got a comment: How about some comments?  Maybe it's just me, but
> I find the code a little hard to comprehend.

The basic algorithm is that you have a list of simple locks that is
treated mostly like a queue. A tread that aquires the Tlock, first
allocates a new lock that is appened to the list and aquired, then
the next to last lock is aquired too.

A thread that releases the lock, releases the first lock and
deletes it from the table and then release the new first
lock.


A thread that specifies a timeout on the release, starts a timer
thread. This timer thread will look if the lock is in the table
and if so releases it and removes it from the table. It also
marks the lock is broken.


> What's up with this?
>
> Exception in thread Thread-456:
> Traceback (most recent call last):
>   File "C:\Python23\lib\threading.py", line 436, in __bootstrap
>     self.run()
>   File "C:\Python23\lib\threading.py", line 544, in run
>     self.function(*self.args, **self.kwargs)
>   File "C:\Python23\Scripts\foo.py", line 29, in break_lock
>     sc.pl.release()
> error: release unlocked lock
>
> That crops up occasionally.

There are two solutions for that.

1) Just remove line 29, it is not needed but I entered it for
   "estetical" concerns.

2) Use semaphores in the table. That means changing the Lock
   to a Semaphore on lines 11 and 38.

> It's been a while, but as I recall, the tricky bit in implementing timed
> locks with an alarm clock thread is seeing to it that threads that don't
> time out waiting on a timed lock do not leave zombie alarm clock threads
> running.  If you are not careful, they could proliferate and eat up system
> resources.

What do you mean with a Zombie? The clock thread will remain for as long
as the timeout, which may be longer then needed but then they disappear,
so there won't be any real zombies.


But I expect some of the python comunity are laughing at me now, because
I have discoverd that the Timer class is implemented by a Condition
variable that is waited upon with a timeout. And this timeout is
implemented by a polling loop.


-- 
Antoon Pardon



More information about the Python-list mailing list