diff options
Diffstat (limited to 'channel.mu')
-rw-r--r-- | channel.mu | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/channel.mu b/channel.mu index a0c2b6dd..0206c2f9 100644 --- a/channel.mu +++ b/channel.mu @@ -1,3 +1,5 @@ +# example program: communicating between routines using channels + recipe producer [ # produce numbers 1 to 5 on a channel default-space:address:array:location <- new location:type, 30:literal @@ -8,7 +10,7 @@ recipe producer [ done?:boolean <- lesser-than n:integer, 5:literal break-unless done?:boolean # other threads might get between these prints - $print [produce: ], n:integer, [ + $print [produce: ], n:integer, [ ] chan:address:channel <- write chan:address:channel, n:integer n:integer <- add n:integer, 1:literal @@ -24,7 +26,7 @@ recipe consumer [ # read an integer from the channel n:integer, chan:address:channel <- read chan:address:channel # other threads might get between these prints - $print [consume: ], n:integer, [ + $print [consume: ], n:integer, [ ] loop } @@ -34,6 +36,8 @@ recipe main [ default-space:address:array:location <- new location:type, 30:literal chan:address:channel <- init-channel 3:literal # create two background 'routines' that communicate by a channel - routine1:integer <- start-running consumer:recipe, chan:address:channel - routine2:integer <- start-running producer:recipe, chan:address:channel + routine1:integer <- start-running producer:recipe, chan:address:channel + routine2:integer <- start-running consumer:recipe, chan:address:channel + wait-for-routine routine1:integer + wait-for-routine routine2:integer ] |