2020-06-17 03:34:16 +03:00
|
|
|
import sync
|
|
|
|
import time
|
|
|
|
|
|
|
|
// Simulate expensive computing using sleep function
|
2020-10-26 14:14:21 +03:00
|
|
|
fn expensive_computing(id int, duration int, mut wg sync.WaitGroup) {
|
|
|
|
println('Executing expensive computing task ($id)...')
|
2021-02-21 18:05:03 +03:00
|
|
|
time.wait(duration * time.millisecond)
|
2020-10-26 14:14:21 +03:00
|
|
|
println('Finish task $id on $duration ms')
|
2020-06-17 03:34:16 +03:00
|
|
|
wg.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-07-24 13:29:47 +03:00
|
|
|
mut wg := sync.new_waitgroup()
|
2020-06-17 03:34:16 +03:00
|
|
|
wg.add(3)
|
2020-07-24 13:29:47 +03:00
|
|
|
go expensive_computing(1, 100, mut wg)
|
|
|
|
go expensive_computing(2, 500, mut wg)
|
|
|
|
go expensive_computing(3, 1000, mut wg)
|
2020-06-17 03:34:16 +03:00
|
|
|
// Join all tasks
|
|
|
|
wg.wait()
|
|
|
|
println('All jobs finished!')
|
|
|
|
}
|