sync: use an atomic counter in test_waitgroup_go in waitgroup_test.v

This commit is contained in:
Delyan Angelov 2025-06-30 21:57:04 +03:00
parent 9957327c37
commit 2c2ded2e0b
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -1,6 +1,7 @@
module sync module sync
import time import time
import sync.stdatomic
fn test_waitgroup_reuse() { fn test_waitgroup_reuse() {
mut wg := new_waitgroup() mut wg := new_waitgroup()
@ -41,14 +42,13 @@ fn test_waitgroup_no_use() {
} }
fn test_waitgroup_go() { fn test_waitgroup_go() {
mut counter := 0 mut counter := stdatomic.new_atomic(0)
p := unsafe { &counter }
mut wg := new_waitgroup() mut wg := new_waitgroup()
for i in 0 .. 10 { for i in 0 .. 10 {
wg.go(fn [p] () { wg.go(fn [mut counter] () {
unsafe { (*p)++ } counter.add(1)
}) })
} }
wg.wait() wg.wait()
assert counter == 10 assert counter.load() == 10
} }