Atomic.load
func Atomic.load[T](a: Atomic[T]): TAtomic.load returns the value in a.
Details
- A
loadthat observes astorefrom another goroutine sees everything that goroutine wrote before thestore. - The value is returned as it is, not copied: a loaded record or array is the same value that the cell holds.
- A
loadfollowed by astoreis two operations, and another goroutine can write between them. UseAtomic.addorAtomic.compareAndSwapto change a value based on the one before it.
Basic examples
Read the value a cell was created with:
let hits = Atomic.new(0)
Atomic.load(hits) // 0Read it again after a store and an add:
let hits = Atomic.new(0)
hits.store(5)
Atomic.load(hits) // 5
hits.add(2) // 7
Atomic.load(hits) // 7Scope
A cell holds a value of any type, such as a string or a float:
let status = Atomic.new("starting")
status.store("ready")
status.load() // "ready"
let ratio = Atomic.new(0.5)
ratio.load() // 0.5A nullable cell returns null until something is stored:
let latest: Atomic[string | null] = Atomic.new(null)
latest.load() // null
latest.store("v2")
latest.load() // "v2"A cell of a union returns whichever member was stored last:
type Idle = { since: int }
type Busy = { job: string }
type State = Idle | Busy
let state: Atomic[State] = Atomic.new(Idle{ since: 0 })
state.load() // Idle{ since: 0 }
state.store(Busy{ job: "backup" })
state.load() // Busy{ job: "backup" }A record comes back as the stored value itself, so in one goroutine a field set through one load shows in the next:
type Point = { x: int, y: int }
let cell = Atomic.new(Point{ x: 1, y: 2 })
let p = cell.load()
p.x = 10
cell.load() // Point{ x: 10, y: 2 }The cell can be the start of a pipe:
let count = Atomic.new(41)
count | Atomic.load() // 41Applications
Count events from many goroutines, then read the total after they finish:
let handled = Atomic.new(0)
let wg = WaitGroup.new()
for _ in Iter.range(0, 100) {
wg.add(1)
go {
defer { wg.done() }
handled.add(1)
}
}
wg.wait()
println(handled.load())100Hand a result back from a goroutine. Load it into a local after the wait, and narrow the local:
let answer: Atomic[string | null] = Atomic.new(null)
let wg = WaitGroup.new()
wg.add(1)
go {
defer { wg.done() }
answer.store("forty-two")
}
wg.wait()
let got = answer.load()
if got != null {
println(got.toUpper())
}FORTY-TWOStop a worker with a flag. The worker loads the flag on every round and returns once it is set:
let stop = Atomic.new(false)
let wg = WaitGroup.new()
wg.add(1)
go {
defer { wg.done() }
while !stop.load() {
Time.sleep(1ms)
}
println("worker stopped")
}
Time.sleep(10ms)
stop.store(true)
wg.wait()worker stoppedPublish a new configuration by storing a whole new record. Each reader loads the current one once and uses that snapshot, so it never sees half of one version and half of another:
type Config = { host: string, port: int }
let config = Atomic.new(Config{ host: "localhost", port: 8080 })
func address(): string {
let c = config.load()
"${c.host}:${c.port}"
}
println(address())
config.store(Config{ host: "example.com", port: 443 })
println(address())localhost:8080
example.com:443Properties and relations
A load returns what the last store put in the cell, and loading changes nothing:
let cell = Atomic.new("a")
cell.store("b")
assert(cell.load() == "b")
assert(cell.load() == cell.load())Atomic.add returns the new value, the same value a load right after it returns:
let n = Atomic.new(10)
let after = n.add(5)
assert(after == n.load())A compareAndSwap that expects the loaded value succeeds when nothing wrote in between:
let n = Atomic.new(3)
let seen = n.load()
assert(n.compareAndSwap(seen, seen * 2))
assert(n.load() == 6)Possible issues
The checker does not narrow a load call, since the next call can return something else:
let name: Atomic[string | null] = Atomic.new("ant")
if name.load() != null {
println(name.load().length())
// error: string | null has no field or function length on every member: narrow the value first
}Load the value into a local once, and narrow the local:
let name: Atomic[string | null] = Atomic.new("ant")
let current = name.load()
if current != null {
println(current.length())
}3A load and a store of its result do not add up to one atomic update. Here two goroutines' steps are written out in the order they can interleave: both load 0, both store 1, and one increment is lost:
let count = Atomic.new(0)
let a = count.load()
let b = count.load()
count.store(a + 1)
count.store(b + 1)
count.load() // 1Atomic.add does the read and the write as one operation, so no increment is lost:
let count = Atomic.new(0)
count.add(1) // 1
count.add(1) // 2
count.load() // 2For an update other than addition, retry compareAndSwap until no other write came between the load and the swap:
let longest = Atomic.new("")
func offer(word: string) {
while true {
let current = longest.load()
if word.length() <= current.length() { return }
if longest.compareAndSwap(current, word) { return }
}
}
let wg = WaitGroup.new()
for word in ["ant", "spider", "bee"] {
wg.add(1)
go {
defer { wg.done() }
offer(word)
}
}
wg.wait()
println(longest.load())spiderA field write on a loaded record is not atomic. The Atomic orders only the cell, so another goroutine that loads the same record can read it while it changes, which is a data race:
type Config = { host: string, port: int }
let config = Atomic.new(Config{ host: "localhost", port: 8080 })
let c = config.load()
c.port = 443 // races with any goroutine that loads and reads configStore a new record instead, built from the loaded one:
type Config = { host: string, port: int }
let config = Atomic.new(Config{ host: "localhost", port: 8080 })
let c = config.load()
config.store(Config{ ...c, port: 443 })
config.load() // Config{ host: "localhost", port: 443 }See also
Atomic.store · Atomic.add · Atomic.compareAndSwap · Atomic.new · Atomic · Mutex.withLock · WaitGroup.wait · Once.value
