vxx / vlib / x / executor / examples / shutdown.v
35 lines · 30 sloc · 617 bytes · e80386b6af8a071a2e13fd654ebcdb2105fb9202
Raw
1import x.executor
2
3fn main() {
4 mut ex := executor.new(queue_size: 2)!
5 ran := chan bool{cap: 1}
6 run_result := chan string{cap: 1}
7
8 ex.try_post(fn [mut ex, ran] () ! {
9 ex.stop()
10 ran <- true
11 })!
12
13 runner := spawn fn [mut ex, run_result] () {
14 ex.run() or {
15 run_result <- err.msg()
16 return
17 }
18 run_result <- 'ok'
19 }()
20
21 did_run := <-ran
22 if !did_run {
23 eprintln('owner stop job did not run')
24 exit(1)
25 }
26 msg := <-run_result
27 if msg != 'ok' {
28 eprintln('run returned error: ${msg}')
29 exit(1)
30 }
31
32 ex.try_post(fn () ! {}) or { println('submit after stop: ${err.msg()}') }
33 ex.wait()!
34 runner.wait()
35}
36