vxx2 / vlib / v / checker / tests / struct_field_mismatch_ref_err.vv
40 lines · 34 sloc · 564 bytes · 0c8ce3bcb9fd4a2e5bd5f991a5a07da976d780d7
Raw
1import sync
2import time
3
4struct SafeCounter {
5mut:
6 mt sync.Mutex = sync.new_mutex()
7pub mut:
8 value map[string]int
9}
10
11fn (mut c SafeCounter) inc(key string) {
12 c.mt.@lock()
13 defer {
14 c.mt.unlock()
15 }
16 c.value[key]++
17}
18
19fn (mut c SafeCounter) value(key string) int {
20 c.mt.@lock()
21 defer {
22 c.mt.unlock()
23 }
24 return c.value[key]
25}
26
27fn main() {
28 mut counter := &SafeCounter{}
29
30 for i := 0; i < 5; i++ {
31 spawn fn [mut counter] () {
32 for j := 0; j < 100; j++ {
33 counter.inc('key')
34 }
35 }()
36 }
37
38 time.sleep(1 * time.second)
39 println(counter.value('key'))
40}
41