1 # Very rudimentary test of thread module
3 # Create a bunch of threads, let each do some work, wait until all are done
5 from test_support
import verbose
10 mutex
= thread
.allocate_lock()
11 rmutex
= thread
.allocate_lock() # for calls to random
13 done
= thread
.allocate_lock()
21 delay
= random
.random() * numtasks
24 print 'task', ident
, 'will run for', round(delay
, 1), 'sec'
27 print 'task', ident
, 'done'
36 global next_ident
, running
38 next_ident
= next_ident
+ 1
40 print 'creating task', next_ident
41 thread
.start_new_thread(task
, (next_ident
,))
45 for i
in range(numtasks
):
48 print 'waiting for all tasks to complete'
50 print 'all tasks done'
53 def __init__(self
, n
):
56 self
.checkin
= thread
.allocate_lock()
57 self
.checkout
= thread
.allocate_lock()
58 self
.checkout
.acquire()
61 checkin
, checkout
= self
.checkin
, self
.checkout
64 self
.waiting
= self
.waiting
+ 1
65 if self
.waiting
== self
.n
:
66 self
.waiting
= self
.n
- 1
72 self
.waiting
= self
.waiting
- 1
81 for i
in range(numtrips
):
83 # give it a good chance to enter the next
84 # barrier before the others are all out
89 delay
= random
.random() * numtasks
92 print 'task', ident
, 'will run for', round(delay
, 1), 'sec'
95 print 'task', ident
, 'entering barrier', i
98 print 'task', ident
, 'leaving barrier', i
100 running
= running
- 1
105 print '\n*** Barrier Test ***'
107 raise ValueError, "'done' should have remained acquired"
108 bar
= barrier(numtasks
)
110 for i
in range(numtasks
):
111 thread
.start_new_thread(task2
, (i
,))
113 print 'all tasks done'