| 1 | import time |
| 2 | |
| 3 | fn fibonacci(c chan int, quit chan int) { |
| 4 | println('fibonacci') |
| 5 | mut x, mut y := 0, 1 |
| 6 | |
| 7 | for { |
| 8 | select { |
| 9 | c <- x { |
| 10 | println('send') |
| 11 | x, y = y, x + y |
| 12 | } |
| 13 | _ = <-quit { |
| 14 | println('quit') |
| 15 | return |
| 16 | } |
| 17 | 2 * time.second { |
| 18 | println('timeout') |
| 19 | return |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn main() { |
| 26 | ch := chan int{} |
| 27 | quit := chan int{} |
| 28 | |
| 29 | spawn fn [ch, quit] () { |
| 30 | for _ in 0 .. 5 { |
| 31 | println('pop') |
| 32 | println(<-ch) |
| 33 | } |
| 34 | println('quit') |
| 35 | quit <- 0 |
| 36 | }() |
| 37 | |
| 38 | fibonacci(ch, quit) |
| 39 | } |
| 40 |