import sync import time struct SafeCounter { mut: mt sync.Mutex = sync.new_mutex() pub mut: value map[string]int } fn (mut c SafeCounter) inc(key string) { c.mt.@lock() defer { c.mt.unlock() } c.value[key]++ } fn (mut c SafeCounter) value(key string) int { c.mt.@lock() defer { c.mt.unlock() } return c.value[key] } fn main() { mut counter := &SafeCounter{} for i := 0; i < 5; i++ { spawn fn [mut counter] () { for j := 0; j < 100; j++ { counter.inc('key') } }() } time.sleep(1 * time.second) println(counter.value('key')) }