1 # example program: communicating between routines using channels
3 def producer sink:&:sink:char -> sink:&:sink:char [
4 # produce characters 1 to 5 on a channel
10 done?:bool <- lesser-than n, 5
12 # other threads might get between these prints
13 $print [produce: ], n, [
22 def consumer source:&:source:char -> source:&:source:char [
23 # consume and print integers from a channel
27 # read an integer from the channel
28 n:char, eof?:bool, source <- read source
30 # other threads might get between these prints
31 $print [consume: ], n:char, [
39 source:&:source:char, sink:&:sink:char <- new-channel 3/capacity
40 # create two background 'routines' that communicate by a channel
41 routine1:num <- start-running producer, sink
42 routine2:num <- start-running consumer, source
43 wait-for-routine routine1
44 wait-for-routine routine2