1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

docs/sync: update to current usage syntax (#8094)

This commit is contained in:
Uwe Krüger 2021-01-13 21:54:11 +01:00 committed by GitHub
parent 30e96528b0
commit dc948e18af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2261,8 +2261,8 @@ Objects can be pushed to channels using the arrow operator. The same operator ca
pop objects from the other end: pop objects from the other end:
```v ```v
mut ch := chan int{} ch := chan int{}
mut ch2 := chan f64{} ch2 := chan f64{}
n := 5 n := 5
x := 7.3 x := 7.3
ch <- n ch <- n
@ -2280,8 +2280,8 @@ associated channel has been closed and the buffer is empty. This situation can b
handled using an or branch (see [Handling Optionals](#handling-optionals)). handled using an or branch (see [Handling Optionals](#handling-optionals)).
```v wip ```v wip
mut ch := chan int{} ch := chan int{}
mut ch2 := chan f64{} ch2 := chan f64{}
// ... // ...
ch.close() ch.close()
// ... // ...
@ -2301,10 +2301,10 @@ of statements - similar to the [match](#match) command:
```v wip ```v wip
import time import time
fn main () { fn main () {
mut c := chan f64{} c := chan f64{}
mut ch := chan f64{} ch := chan f64{}
mut ch2 := chan f64{} ch2 := chan f64{}
mut ch3 := chan f64{} ch3 := chan f64{}
mut b := 0.0 mut b := 0.0
// ... // ...
select { select {
@ -2351,16 +2351,16 @@ struct Abc {
} }
a := 2.13 a := 2.13
mut ch := chan f64{} ch := chan f64{}
res := ch.try_push(a) // try to perform `ch <- a` res := ch.try_push(a) // try to perform `ch <- a`
println(res) println(res)
l := ch.len // number of elements in queue l := ch.len // number of elements in queue
c := ch.cap // maximum queue length c := ch.cap // maximum queue length
println(l) println(l)
println(c) println(c)
// mut b := Abc{} mut b := Abc{}
// mut ch2 := chan f64{} ch2 := chan Abc{}
// res2 := ch2.try_pop(mut b) // try to perform `b = <-ch2 res2 := ch2.try_pop(b) // try to perform `b = <-ch2`
``` ```
The `try_push/pop()` methods will return immediately with one of the results The `try_push/pop()` methods will return immediately with one of the results
@ -2370,33 +2370,31 @@ Usage of these methods and properties in production is not recommended -
algorithms based on them are often subject to race conditions. Use `select` instead. algorithms based on them are often subject to race conditions. Use `select` instead.
Data can be exchanged between a coroutine and the calling thread via a shared variable. Data can be exchanged between a coroutine and the calling thread via a shared variable.
Such variables should be created as references and passed to the coroutine as `mut`. Such variables should be created as `shared` and passed to the coroutine as such, too.
The underlying `struct` should also contain a `mutex` to lock concurrent access: The underlying `struct` contains a hidden *mutex* that allows locking concurrent access
using `rlock` for read-only and `lock` for read/write access.
```v ```v
import sync
struct St { struct St {
mut: mut:
x int // share data x int // data to shared
mtx &sync.Mutex
} }
fn (mut b St) g() { fn (shared b St) g() {
b.mtx.m_lock() lock b {
// read/modify/write b.x // read/modify/write b.x
b.mtx.unlock() }
} }
fn caller() { fn main() {
mut a := &St{ // create as reference so it's on the heap shared a := &St{ // create as reference so it's on the heap
x: 10 x: 10
mtx: sync.new_mutex()
} }
go a.g() go a.g()
a.mtx.m_lock() // ...
// read/modify/write a.x rlock a {
a.mtx.unlock() // read a.x
}
} }
``` ```