| 1 | import time |
| 2 | |
| 3 | fn work(input chan u32, started chan bool) { |
| 4 | println('> work started') |
| 5 | started <- true |
| 6 | for { |
| 7 | x := <-input or { break } |
| 8 | println('> work x: ${x}') |
| 9 | time.sleep(50 * time.millisecond) |
| 10 | } |
| 11 | println('> work ended') |
| 12 | } |
| 13 | |
| 14 | fn main() { |
| 15 | println('> main start') |
| 16 | ch := chan u32{cap: 100} |
| 17 | work_started := chan bool{} |
| 18 | for x in 0 .. 10 { |
| 19 | ch <- x |
| 20 | } |
| 21 | task := spawn work(ch, work_started) |
| 22 | _ := <-work_started |
| 23 | |
| 24 | ch.close() |
| 25 | // println('> main ch.close called') // the position of this is not deterministic |
| 26 | |
| 27 | task.wait() |
| 28 | println('> main task was finished') |
| 29 | } |
| 30 | |