.\" Made by website/plain/build.mjs from website/words/atomic.md; change that file. .TH SG-ATOMIC 7 2026-11-28 "SG" "SG reference" .SH NAME atomic \- a number or a bool that tasks change without a lock, one step at a time .SH SYNOPSIS .TP \fBatomic T\fR the type: T is an integer, a bool, a float, a double or a raw T .TP \fBatomic 0\fR a value of it .TP \fBnew shared atomic 0\fR one that several tasks own .TP \fBx.load(), x.store(v)\fR reads it, writes it .TP \fBx.fetchAdd(n), x.exchange(v)\fR changes it in one step, and gives the value before .TP \fBx.compareExchange(expected, desired)\fR writes only if it still holds expected .TP \fBx.load(MemoryOrder.Acquire)\fR with an order written last .SH DESCRIPTION An atomic value is changed by the processor in one step, so two tasks that change it at the same time never lose one of the changes. For a counter or a flag that is cheaper than a lock. Every method works through \fBread\fR, so tasks share one as a \fBshared atomic T\fR and need no \fBlock\fR around it. .PP Each operation is sequentially consistent unless an order is written last: \fBRelaxed\fR, \fBAcquire\fR, \fBRelease\fR, \fBAcquireRelease\fR or \fBSequentiallyConsistent\fR, the variants of \fBMemoryOrder\fR\&. An order the operation cannot keep is a compile error. .SH EXAMPLES .SS "A counter" .RS 4 .nf void main() { var count = atomic 0 _ = count.fetchAdd(1) _ = count.fetchAdd(1) print(count.load()) } .fi .RE .PP .RS 4 .nf 2 .fi .RE .PP \fBfetchAdd\fR gives the value before it added. \fB_ =\fR says that value is not needed: SG does not let a value be dropped without a word. .SS "Shared by two tasks" .RS 4 .nf void bump(int times, shared atomic int total) { for _ in 0..times { _ = total.fetchAdd(1) } } void main() { var total = new shared atomic 0 var a = Task.run(bump, 500, share total) var b = Task.run(bump, 500, share total) a.wait() b.wait() print(total.load()) } .fi .RE .PP .RS 4 .nf 1000 .fi .RE .PP \fBshare total\fR gives each task one more owner of the same value. No addition is lost. .SS "Write only if nothing changed it" .RS 4 .nf void main() { var state = atomic 9 match state.compareExchange(9, 10) { Ok(was) => print($"wrote over {was}") Error(found) => print($"found {found}") } match state.compareExchange(9, 11) { Ok(was) => print($"wrote over {was}") Error(found) => print($"found {found}") } } .fi .RE .PP .RS 4 .nf wrote over 9 found 10 .fi .RE .PP The second call expected 9 but found 10, so it wrote nothing. This is the step under most structures built without locks. .SS "A value handed from one task to another" .RS 4 .nf void publish(shared atomic int data, shared atomic bool ready) { data.store(42, MemoryOrder.Relaxed) ready.store(true, MemoryOrder.Release) } void main() { var data = new shared atomic 0 var ready = new shared atomic false var writer = Task.run(publish, share data, share ready) while !ready.load(MemoryOrder.Acquire) { Atomic.spinLoop() } print(data.load(MemoryOrder.Relaxed)) writer.wait() } .fi .RE .PP .RS 4 .nf 42 .fi .RE .PP \fBRelease\fR on the store and \fBAcquire\fR on the load make what was written before the store seen after the load. \fBAtomic.spinLoop()\fR tells the processor the loop is waiting. .SH "WHAT THE COMPILER REFUSES" .SS "A change in two steps" .RS 4 .nf void main() { var count = atomic 5 count += 1 } .fi .RE .PP .RS 4 .nf error: `+=` would read `count` and write it in two steps, and another task could change it between them --> example.sg:3:5 3 | count += 1 | ^^^^^^^^^^ hint: one step: `_ = count.fetchAdd(1)` .fi .RE .SS "An order the operation cannot keep" .RS 4 .nf void main() { var count = atomic 5 print(count.load(MemoryOrder.Release)) } .fi .RE .PP .RS 4 .nf error: `load` cannot keep the order `Release` a load reads, so its order is `Relaxed`, `Acquire` or `SequentiallyConsistent` --> example.sg:3:22 3 | print(count.load(MemoryOrder.Release)) | ^^^^^^^^^^^^^^^^^^^ .fi .RE .SS "A value the processor cannot change in one step" .RS 4 .nf void main() { var name = atomic "Pasha" } .fi .RE .PP .RS 4 .nf error: an atomic value is an integer, a `bool`, a `float`, a `double` or a raw pointer, and this is `string` what the processor changes in one step; a larger value that several tasks change is a `shared locked T` --> example.sg:2:16 2 | var name = atomic "Pasha" | ^^^^^^^^^^^^^^ .fi .RE .SH "SEE ALSO" .BR sg-shared (7), .BR sg-locked (7), .BR sg-lock (7) .PP https://sg\-lang.org/reference/atomic.html