vxx / vlib / x / executor / examples / run_sync.v
38 lines · 33 sloc · 682 bytes · e80386b6af8a071a2e13fd654ebcdb2105fb9202
Raw
1import x.executor
2
3fn main() {
4 mut ex := executor.new(queue_size: 4)!
5 result := chan string{cap: 1}
6 run_result := chan string{cap: 1}
7
8 runner := spawn fn [mut ex, run_result] () {
9 ex.run() or {
10 run_result <- err.msg()
11 return
12 }
13 run_result <- 'ok'
14 }()
15
16 caller := spawn fn [mut ex, result] () {
17 ex.run_sync(fn [result] () ! {
18 result <- 'ran on owner loop'
19 }) or { result <- 'run_sync failed: ${err.msg()}' }
20 }()
21
22 msg := <-result
23 if msg != 'ran on owner loop' {
24 eprintln(msg)
25 exit(1)
26 }
27 println(msg)
28
29 ex.stop()
30 run_msg := <-run_result
31 if run_msg != 'ok' {
32 eprintln('run failed: ${run_msg}')
33 exit(1)
34 }
35 ex.wait()!
36 caller.wait()
37 runner.wait()
38}
39