v2 / vlib / v / tests / concurrency / tcc_atomic_postfix_test.v
24 lines · 21 sloc · 429 bytes · 64f89f329d7a86d6a9781cf7f7eb231b72a3683a
Raw
1// vtest build: tinyc && linux
2// vtest vflags: -cc tcc -no-retry-compilation
3module main
4
5struct Counter {
6mut:
7 value atomic int
8}
9
10fn (mut c Counter) add_many(n int) {
11 for _ in 0 .. n {
12 c.value++
13 }
14}
15
16fn test_tcc_atomic_postfix_is_atomic() {
17 mut counter := &Counter{}
18 mut threads := []thread{cap: 8}
19 for _ in 0 .. 8 {
20 threads << spawn counter.add_many(200_000)
21 }
22 threads.wait()
23 assert counter.value == 1_600_000
24}
25