1 # Very rudimentary test of thread module
3 # Create a bunch of threads, let each do some work, wait until all are done
9 mutex
= thread
.allocate_lock()
10 whmutex
= thread
.allocate_lock() # for calls to whrandom
12 done
= thread
.allocate_lock()
20 delay
= whrandom
.random() * numtasks
22 print 'task', ident
, 'will run for', delay
, 'sec'
24 print 'task', ident
, 'done'
33 global next_ident
, running
35 next_ident
= next_ident
+ 1
36 print 'creating task', next_ident
37 thread
.start_new_thread(task
, (next_ident
,))
41 for i
in range(numtasks
):
44 print 'waiting for all tasks to complete'
46 print 'all tasks done'
49 def __init__(self
, n
):
52 self
.checkin
= thread
.allocate_lock()
53 self
.checkout
= thread
.allocate_lock()
54 self
.checkout
.acquire()
57 checkin
, checkout
= self
.checkin
, self
.checkout
60 self
.waiting
= self
.waiting
+ 1
61 if self
.waiting
== self
.n
:
62 self
.waiting
= self
.n
- 1
68 self
.waiting
= self
.waiting
- 1
77 for i
in range(numtrips
):
79 # give it a good chance to enter the next
80 # barrier before the others are all out
85 delay
= whrandom
.random() * numtasks
87 print 'task', ident
, 'will run for', delay
, 'sec'
89 print 'task', ident
, 'entering barrier', i
91 print 'task', ident
, 'leaving barrier', i
98 print '\n*** Barrier Test ***'
100 raise ValueError, "'done' should have remained acquired"
101 bar
= barrier(numtasks
)
103 for i
in range(numtasks
):
104 thread
.start_new_thread(task2
, (i
,))
106 print 'all tasks done'