Idle wait for outstanding threads?

Alan Kennedy alanmk at hotmail.com
Wed Jan 30 08:18:47 EST 2002


Greetings All,

I'm trying to figure out how to do an idle wait while waiting for a
number of threads to finish. The scenario is as follows.

>From a main thread, I start a number of threads to execute some
service, say some database queries. I want the main thread to wait
until all of the service threads are complete before continuing.

Solution 1. Use a simple integer counter, preotected by a simple
thread.lock. When a service thread is started, it increments the
counter ('acquire'ing the lock before and 'release'ing it afterwards).
When a service thread finishes, it decrements the counter. The main
thread simply cycles in a loop, waiting for the counter to reach zero.
The problem with this is that the main thread has to execute a "busy
wait", i.e. it enters a while loop something like this

while 1:
	lock.acquire()
	if counter == 0:
		break
	lock.release()

This seems very wasteful of CPU cycles.

Solution 2. Use a Semaphore? Semaphores are used to "guard resources
with limited capacity", e.g. file descriptors. So if I have 20 file
descriptors available, I initialise my semaphore with the value 20.
Every time a thread wants a file descriptor, it 'acquire's the
semaphore, which decrements the counter. When its finished, it
'release's the semaphore, which increments the counter again. So the
semaphore counter represents the number of free resources available.
When the semaphore counter hits zero, an 'acquire' call will block
until a resource becomes free, i.e. the counter is non-zero. The block
is an "idle" block, i.e. it doesn't involve a CPU-hogging while loop.

This is the behaviour I want while waiting for outstanding service
threads. Except that I want to wait for the counter to represent
outstanding threads, and to wait until it is zero, and block while it
is non-zero: i.e. precisely the opposite that semaphores were designed
for.

I can't think of a way to make a semaphore do what I need. 

Does anyone have any suggestions on how to do it? Or how to solve the
problem in a different way using python libraries?

Thanks in advance,

Alan.



More information about the Python-list mailing list