aUCBLogo Demos and Tests / testfiber
be testfiber
; make "A 100
; print :A
; proc1 101
;stop
clearScreen
perspective
hideTurtle
GraphLeave
Thread [makeFibers]
end
be makeFibers
; Fiber [randomWalk]
; print [OK]
;stop
; ignore readchar
; Fiber [graphenter morph graphleave]
; print 5
; Fiber [while [readchar != "q][pr # waitms 1000]]
n=10
counter=IntArray n
GraphEnter
for [i 1 n]
[
; Fiber (List "randomWalk i n)
; Fiber List "count_silently i
Fiber List "count_a_bit i*100
; waitms random 10
]
print 6
; Fiber [rotatescene2]
repeat 255 []
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
WindowMode
; hideTurtle
setPenSize 0
setPenColor HSB 360*col/n 1 1
rightRoll random 360
right random 360
repeat 1000
[ forward 10
right (rnd-0.5)*90
rightRoll (rnd-0.5)*360
updategraph
; waitms 10
Yield
]
end
to proc1 :B
print :B
Fiber [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