v2 / vlib / v / slow_tests / inout / push_work_on_channel.vv
29 lines · 25 sloc · 565 bytes · 6a32c810703f4ec0c39fe18298ebe6c40acac8f1
Raw
1import time
2
3fn 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
14fn 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