mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
sync: add implementation for WaitGroup.go/1, add test (#24797)
This commit is contained in:
parent
3d320afa65
commit
f62b5fd7f2
@ -82,3 +82,14 @@ pub fn (mut wg WaitGroup) wait() {
|
|||||||
C.atomic_fetch_add_u32(voidptr(&wg.wait_count), 1)
|
C.atomic_fetch_add_u32(voidptr(&wg.wait_count), 1)
|
||||||
wg.sem.wait() // blocks until task_count becomes 0
|
wg.sem.wait() // blocks until task_count becomes 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go starts `f` in a new thread, arranging to call wg.add(1) before that,
|
||||||
|
// and wg.done() in the same thread. The function `f` should not panic.
|
||||||
|
// Calls to wg.go() should happen before the call to wg.wait().
|
||||||
|
pub fn (mut wg WaitGroup) go(f fn ()) {
|
||||||
|
wg.add(1)
|
||||||
|
spawn fn (mut wg WaitGroup, f fn ()) {
|
||||||
|
f()
|
||||||
|
wg.done()
|
||||||
|
}(mut wg, f)
|
||||||
|
}
|
||||||
|
@ -39,3 +39,16 @@ fn test_waitgroup_no_use() {
|
|||||||
wg.wait()
|
wg.wait()
|
||||||
done = true
|
done = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_waitgroup_go() {
|
||||||
|
mut counter := 0
|
||||||
|
p := unsafe { &counter }
|
||||||
|
mut wg := new_waitgroup()
|
||||||
|
for i in 0 .. 10 {
|
||||||
|
wg.go(fn [p] () {
|
||||||
|
unsafe { (*p)++ }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
wg.wait()
|
||||||
|
assert counter == 10
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user