mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
23 lines
407 B
V
23 lines
407 B
V
import sync
|
|
|
|
fn test_spinlock() {
|
|
mut counter := 0
|
|
mut s := sync.new_spin_lock()
|
|
num_threads := 10
|
|
mut wg := sync.new_waitgroup()
|
|
wg.add(num_threads)
|
|
|
|
for _ in 0 .. num_threads {
|
|
spawn fn (mut wg sync.WaitGroup, s &sync.SpinLock, counter_ref &int) {
|
|
defer {
|
|
s.unlock()
|
|
wg.done()
|
|
}
|
|
s.lock()
|
|
(*counter_ref)++
|
|
}(mut wg, s, &counter)
|
|
}
|
|
wg.wait()
|
|
assert counter == num_threads
|
|
}
|