[Tutor] locks and threads

James jtp at nc.rr.com
Sat Aug 2 20:36:00 CEST 2008


All,

I'm trying to write a class that will acquire a lock before entering a
critical section, and then release it. Does this look like the right
way to go about accomplishing my goal?

try:
grabLock = self.lock.acquire( 0 )
	if grabLock:
		print 'acquired lock successfully'
	else:
		print "did *not* obtain lock"
		< ** what do I put here? ** >
finally:
	if grabLock is True:

		<do something "critical" here>
		self.lock.release()
		print 'released lock'

What should I be doing in the else: statement? If I can't grab the
lock, should I simply try again? Maybe a while loop that keeps trying
until I grab the lock? (I'm not really sure how I'm going to integrate
the 'try' statement with the while loop, though, to solve the problem
of not grabbing the lock)

Thoughts?

-j


More information about the Tutor mailing list