v/examples/concurrency/concurrency.v

22 lines
528 B
V
Raw Normal View History

2020-06-17 02:34:16 +02:00
import sync
import time
// Simulate expensive computing using sleep function
2020-07-24 12:29:47 +02:00
fn expensive_computing(id, duration int, mut wg sync.WaitGroup) {
2020-06-17 02:34:16 +02:00
println('Executing expensive computing task (${id})...')
time.sleep_ms(duration)
println('Finish task ${id} on ${duration} ms')
wg.done()
}
fn main() {
2020-07-24 12:29:47 +02:00
mut wg := sync.new_waitgroup()
2020-06-17 02:34:16 +02:00
wg.add(3)
2020-07-24 12:29:47 +02: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 02:34:16 +02:00
// Join all tasks
wg.wait()
println('All jobs finished!')
}