I64
#struct I64A 64-bit whole number shared between threads.
| Name | Type | Description |
|---|---|---|
cell | i64* | The address of the value. |
mu | sync.Mutex | The internal lock. |
import atomic
mut counter := atomic.I64 {}
func worker():
repeat 1000:
atomic.Add(counter, 1)
end
end
func main:
counter = atomic.NewI64(0)
a := spawn(worker)
b := spawn(worker)
join(a)
join(b)
println atomic.Load(counter)
atomic.Store(counter, 10)
println atomic.Swap(counter, 20), atomic.Load(counter)
println atomic.CompareAndSwap(counter, 20, 30),
atomic.CompareAndSwap(counter, 20, 40),
atomic.Load(counter)
atomic.DestroyI64(counter)
flag := atomic.NewFlag(false)
println atomic.TestAndSet(flag), atomic.TestAndSet(flag), atomic.Get(flag)
atomic.Set(flag, false)
println atomic.Get(flag)
atomic.DestroyFlag(flag)
end2000 10 20 true false 30 true false true false