docs: fix channel select example (#10213)
parent
539594bfce
commit
25645dbc44
doc
63
doc/docs.md
63
doc/docs.md
|
@ -2885,29 +2885,48 @@ y := <-ch2 ?
|
||||||
The `select` command allows monitoring several channels at the same time
|
The `select` command allows monitoring several channels at the same time
|
||||||
without noticeable CPU load. It consists of a list of possible transfers and associated branches
|
without noticeable CPU load. It consists of a list of possible transfers and associated branches
|
||||||
of statements - similar to the [match](#match) command:
|
of statements - similar to the [match](#match) command:
|
||||||
```v wip
|
```v
|
||||||
import time
|
import time
|
||||||
fn main () {
|
|
||||||
c := chan f64{}
|
fn main() {
|
||||||
ch := chan f64{}
|
ch := chan f64{}
|
||||||
ch2 := chan f64{}
|
ch2 := chan f64{}
|
||||||
ch3 := chan f64{}
|
ch3 := chan f64{}
|
||||||
mut b := 0.0
|
mut b := 0.0
|
||||||
// ...
|
c := 1.0
|
||||||
select {
|
// ... setup go threads that will send on ch/ch2
|
||||||
a := <-ch {
|
go fn (the_channel chan f64) {
|
||||||
// do something with `a`
|
time.sleep(5 * time.millisecond)
|
||||||
}
|
the_channel <- 1.0
|
||||||
b = <-ch2 {
|
}(ch)
|
||||||
// do something with predeclared variable `b`
|
go fn (the_channel chan f64) {
|
||||||
}
|
time.sleep(1 * time.millisecond)
|
||||||
ch3 <- c {
|
the_channel <- 1.0
|
||||||
// do something if `c` was sent
|
}(ch2)
|
||||||
}
|
go fn (the_channel chan f64) {
|
||||||
> 500 * time.millisecond {
|
_ := <-the_channel
|
||||||
// do something if no channel has become ready within 0.5s
|
}(ch3)
|
||||||
}
|
//
|
||||||
}
|
select {
|
||||||
|
a := <-ch {
|
||||||
|
// do something with `a`
|
||||||
|
eprintln('> a: $a')
|
||||||
|
}
|
||||||
|
b = <-ch2 {
|
||||||
|
// do something with predeclared variable `b`
|
||||||
|
eprintln('> b: $b')
|
||||||
|
}
|
||||||
|
ch3 <- c {
|
||||||
|
// do something if `c` was sent
|
||||||
|
time.sleep(5 * time.millisecond)
|
||||||
|
eprintln('> c: $c was send on channel ch3')
|
||||||
|
}
|
||||||
|
> 500 * time.millisecond {
|
||||||
|
// do something if no channel has become ready within 0.5s
|
||||||
|
eprintln('> more than 0.5s passed without a channel being ready')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eprintln('> done')
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue