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

docs: update concurrency docs and examples to use []thread{} (#8933)

This commit is contained in:
Miccah 2021-02-24 03:41:12 -06:00 committed by GitHub
parent 9e06af8bf9
commit 66c85aa5cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 21 deletions

View File

@ -2566,28 +2566,24 @@ fn main() {
} }
``` ```
If there is a large number of tasks that do not return a value it might be easier to manage If there is a large number of tasks, it might be easier to manage them
them using a wait group. However, for this approach the function(s) called concurrently have using an array of threads.
to be designed with this wait group in mind:
```v ```v
import sync
import time import time
fn task(id int, duration int, mut wg sync.WaitGroup) { fn task(id int, duration int) {
println('task $id begin') println('task $id begin')
time.wait(duration * time.millisecond) time.wait(duration * time.millisecond)
println('task $id end') println('task $id end')
wg.done()
} }
fn main() { fn main() {
mut wg := sync.new_waitgroup() mut threads := []thread{}
wg.add(3) threads << go task(1, 500)
go task(1, 500, mut wg) threads << go task(2, 900)
go task(2, 900, mut wg) threads << go task(3, 100)
go task(3, 100, mut wg) threads.wait()
wg.wait()
println('done') println('done')
} }
@ -2601,6 +2597,27 @@ fn main() {
// done // done
``` ```
Additionally for threads that return the same type, calling `wait()`
on the thread array will return all computed values.
```v
fn expensive_computing(i int) int {
return i * i
}
fn main() {
mut threads := []thread int{}
for i in 1 .. 10 {
threads << go expensive_computing(i)
}
// Join all tasks
r := threads.wait()
println('All jobs finished: $r')
}
// Output: All jobs finished: [1, 4, 9, 16, 25, 36, 49, 64, 81]
```
### Channels ### Channels
Channels are the preferred way to communicate between coroutines. V's channels work basically like Channels are the preferred way to communicate between coroutines. V's channels work basically like
those in Go. You can push objects into a channel on one end and pop objects from the other end. those in Go. You can push objects into a channel on one end and pop objects from the other end.

View File

@ -1,21 +1,18 @@
import sync
import time import time
// Simulate expensive computing using sleep function // Simulate expensive computing using sleep function
fn expensive_computing(id int, duration int, mut wg sync.WaitGroup) { fn expensive_computing(id int, duration int) {
println('Executing expensive computing task ($id)...') println('Executing expensive computing task ($id)...')
time.wait(duration * time.millisecond) time.wait(duration * time.millisecond)
println('Finish task $id on $duration ms') println('Finish task $id on $duration ms')
wg.done()
} }
fn main() { fn main() {
mut wg := sync.new_waitgroup() mut threads := []thread{}
wg.add(3) threads << go expensive_computing(1, 100)
go expensive_computing(1, 100, mut wg) threads << go expensive_computing(2, 500)
go expensive_computing(2, 500, mut wg) threads << go expensive_computing(3, 1000)
go expensive_computing(3, 1000, mut wg)
// Join all tasks // Join all tasks
wg.wait() threads.wait()
println('All jobs finished!') println('All jobs finished!')
} }

View File

@ -0,0 +1,13 @@
fn expensive_computing(i int) int {
return i * i
}
fn main() {
mut threads := []thread int{}
for i in 1 .. 10 {
threads << go expensive_computing(i)
}
// Join all tasks
r := threads.wait()
println('All jobs finished: $r')
}