mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
docs: update concurrency docs and examples to use []thread{} (#8933)
This commit is contained in:
parent
9e06af8bf9
commit
66c85aa5cb
3 changed files with 48 additions and 21 deletions
|
@ -1,21 +1,18 @@
|
|||
import sync
|
||||
import time
|
||||
|
||||
// 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)...')
|
||||
time.wait(duration * time.millisecond)
|
||||
println('Finish task $id on $duration ms')
|
||||
wg.done()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut wg := sync.new_waitgroup()
|
||||
wg.add(3)
|
||||
go expensive_computing(1, 100, mut wg)
|
||||
go expensive_computing(2, 500, mut wg)
|
||||
go expensive_computing(3, 1000, mut wg)
|
||||
mut threads := []thread{}
|
||||
threads << go expensive_computing(1, 100)
|
||||
threads << go expensive_computing(2, 500)
|
||||
threads << go expensive_computing(3, 1000)
|
||||
// Join all tasks
|
||||
wg.wait()
|
||||
threads.wait()
|
||||
println('All jobs finished!')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue