vxx / vlib / x / executor / examples / bounded_admission.v
30 lines · 24 sloc · 549 bytes · e80386b6af8a071a2e13fd654ebcdb2105fb9202
Raw
1import time
2import x.executor
3
4fn main() {
5 mut ex := executor.new(queue_size: 1)!
6 ran := chan string{cap: 2}
7
8 ex.try_post(fn [ran] () ! {
9 ran <- 'first'
10 })!
11
12 ex.try_post(fn () ! {}) or { println('try_post backpressure: ${err.msg()}') }
13
14 ex.post_with_timeout(20 * time.millisecond, fn () ! {}) or {
15 println('timeout bounded admission: ${err.msg()}')
16 }
17
18 if !ex.run_one()! {
19 eprintln('queued job did not run')
20 exit(1)
21 }
22 println(<-ran)
23
24 ex.stop()
25 if ex.run_one()! {
26 eprintln('unexpected job after stop')
27 exit(1)
28 }
29 ex.wait()!
30}
31