1994-04-14 20:28:41 +00:00
|
|
|
# Very rudimentary test of thread module
|
|
|
|
|
|
|
|
# Create a bunch of threads, let each do some work, wait until all are done
|
|
|
|
|
1997-01-13 22:54:27 +00:00
|
|
|
from test_support import verbose
|
1994-04-14 20:28:41 +00:00
|
|
|
import whrandom
|
|
|
|
import thread
|
|
|
|
import time
|
|
|
|
|
|
|
|
mutex = thread.allocate_lock()
|
1994-05-23 12:17:36 +00:00
|
|
|
whmutex = thread.allocate_lock() # for calls to whrandom
|
1994-04-14 20:28:41 +00:00
|
|
|
running = 0
|
|
|
|
done = thread.allocate_lock()
|
|
|
|
done.acquire()
|
|
|
|
|
1994-05-23 12:17:36 +00:00
|
|
|
numtasks = 10
|
|
|
|
|
1994-04-14 20:28:41 +00:00
|
|
|
def task(ident):
|
|
|
|
global running
|
1994-05-23 12:17:36 +00:00
|
|
|
whmutex.acquire()
|
|
|
|
delay = whrandom.random() * numtasks
|
|
|
|
whmutex.release()
|
1996-12-16 23:42:35 +00:00
|
|
|
if verbose:
|
1997-07-18 23:50:22 +00:00
|
|
|
print 'task', ident, 'will run for', round(delay, 1), 'sec'
|
1994-04-14 20:28:41 +00:00
|
|
|
time.sleep(delay)
|
1996-12-16 23:42:35 +00:00
|
|
|
if verbose:
|
|
|
|
print 'task', ident, 'done'
|
1994-04-14 20:28:41 +00:00
|
|
|
mutex.acquire()
|
|
|
|
running = running - 1
|
|
|
|
if running == 0:
|
|
|
|
done.release()
|
|
|
|
mutex.release()
|
|
|
|
|
|
|
|
next_ident = 0
|
|
|
|
def newtask():
|
|
|
|
global next_ident, running
|
|
|
|
mutex.acquire()
|
|
|
|
next_ident = next_ident + 1
|
1996-12-16 23:42:35 +00:00
|
|
|
if verbose:
|
|
|
|
print 'creating task', next_ident
|
1994-04-14 20:28:41 +00:00
|
|
|
thread.start_new_thread(task, (next_ident,))
|
|
|
|
running = running + 1
|
|
|
|
mutex.release()
|
|
|
|
|
1994-05-23 12:17:36 +00:00
|
|
|
for i in range(numtasks):
|
1994-04-14 20:28:41 +00:00
|
|
|
newtask()
|
|
|
|
|
|
|
|
print 'waiting for all tasks to complete'
|
|
|
|
done.acquire()
|
|
|
|
print 'all tasks done'
|
1994-05-23 12:17:36 +00:00
|
|
|
|
|
|
|
class barrier:
|
|
|
|
def __init__(self, n):
|
|
|
|
self.n = n
|
|
|
|
self.waiting = 0
|
|
|
|
self.checkin = thread.allocate_lock()
|
|
|
|
self.checkout = thread.allocate_lock()
|
|
|
|
self.checkout.acquire()
|
|
|
|
|
|
|
|
def enter(self):
|
|
|
|
checkin, checkout = self.checkin, self.checkout
|
|
|
|
|
|
|
|
checkin.acquire()
|
|
|
|
self.waiting = self.waiting + 1
|
|
|
|
if self.waiting == self.n:
|
|
|
|
self.waiting = self.n - 1
|
|
|
|
checkout.release()
|
|
|
|
return
|
|
|
|
checkin.release()
|
|
|
|
|
|
|
|
checkout.acquire()
|
|
|
|
self.waiting = self.waiting - 1
|
|
|
|
if self.waiting == 0:
|
|
|
|
checkin.release()
|
|
|
|
return
|
|
|
|
checkout.release()
|
|
|
|
|
|
|
|
numtrips = 3
|
|
|
|
def task2(ident):
|
|
|
|
global running
|
|
|
|
for i in range(numtrips):
|
|
|
|
if ident == 0:
|
|
|
|
# give it a good chance to enter the next
|
|
|
|
# barrier before the others are all out
|
|
|
|
# of the current one
|
|
|
|
delay = 0.001
|
|
|
|
else:
|
|
|
|
whmutex.acquire()
|
|
|
|
delay = whrandom.random() * numtasks
|
|
|
|
whmutex.release()
|
1996-12-16 23:42:35 +00:00
|
|
|
if verbose:
|
1997-07-18 23:50:22 +00:00
|
|
|
print 'task', ident, 'will run for', round(delay, 1), 'sec'
|
1994-05-23 12:17:36 +00:00
|
|
|
time.sleep(delay)
|
1996-12-16 23:42:35 +00:00
|
|
|
if verbose:
|
|
|
|
print 'task', ident, 'entering barrier', i
|
1994-05-23 12:17:36 +00:00
|
|
|
bar.enter()
|
1996-12-16 23:42:35 +00:00
|
|
|
if verbose:
|
|
|
|
print 'task', ident, 'leaving barrier', i
|
1994-05-23 12:17:36 +00:00
|
|
|
mutex.acquire()
|
|
|
|
running = running - 1
|
|
|
|
if running == 0:
|
|
|
|
done.release()
|
|
|
|
mutex.release()
|
|
|
|
|
|
|
|
print '\n*** Barrier Test ***'
|
|
|
|
if done.acquire(0):
|
|
|
|
raise ValueError, "'done' should have remained acquired"
|
|
|
|
bar = barrier(numtasks)
|
|
|
|
running = numtasks
|
|
|
|
for i in range(numtasks):
|
|
|
|
thread.start_new_thread(task2, (i,))
|
|
|
|
done.acquire()
|
|
|
|
print 'all tasks done'
|