aUCBLogo Demos and Tests / testthread
be testthread
; make "A 100
; print :A
; proc1 101
;stop
clearScreen
hideTurtle
GraphLeave
; thread [randomWalk]
; print [OK]
;stop
; ignore readchar
; thread [graphenter morph graphleave]
; print 5
; thread [while [readchar != "q][pr # waitms 1000]]
n=10
counter=IntArray n
for [i 1 n]
[
thread (List "randomWalk i n)
; thread List "count_silently i
; thread List "count_a_bit i*100
; waitms random 10
]
; print 6
end
to count_a_bit start
for [i start start+99]
[ print i
; Yield
; dispatchMessages
; waitms random 10
]
end
to count_silently index
for [i 1 100000]
[ counter.index=counter.index+1
]
end
be randomWalk col n
setTurtle newTurtle
; hideTurtle
setPenColor HSB 360*col/n 1 1
right random 360
repeat 100
[ forward 10
right (rnd-0.5)*90
updategraph
; waitms 10
Yield
]
end
to proc1 :B
print :B
Thread [proc2] ; A new thread is created, it call 'proc2'
make "A 200 ; Note: only first thread will continue here
make "B 102
proc2
end
to proc2
print :A ; A is a global variable
print :B ; B is a local variable (parameter of proc1)
end