[Zope-dev] Threads and locks

Itamar Shtull-Trauring itamars@ibm.net
Tue, 25 Apr 2000 11:01:48 +0300


I want to limit access to instances of a class I have - "Debt" - so that
only one thread at a time will access a given instance.  Debt is used to
keep tracking of how much people owe us, so I need to make sure they don't
get charged twice, etc..

For example, the function:

	def dostuff(self):
		# only one thread at a time should call these two lines:
		sum = self.debtItem.get()
		self.debtItem.add(23 + sum)

Now, I could just use a global lock for all threads who call this function,
but that would be a waste since I only don't want two threads accessing the
same instance of Debt at the same time, not all instances.

In other words, if I have two instance of Debt called A and B, threads can
access both A and B at the same time, but you can't have two thread
accessing A at the same time.

I could add a lock to each instance of Debt.  However, each thread has it's
own copy of all Persistent objects.  So each thread has its own copy of the
lock.  If I do A.lock.acquire() in one thread, will another thread be
acquiring the same lock if it does A.lock.acquire()?

-- 
Itamar S.T.  itamars@ibm.net