v2 / vlib / x / atomics / examples / basic.v
23 lines · 17 sloc · 600 bytes · edd4a7129fa45c0bb5d5923b5b1a64cb5053ce46
Raw
1module main
2
3import x.atomics
4
5fn main() {
6 // Basic atomic load and store operations
7 mut value := i32(0)
8
9 // Atomically store a value
10 atomics.store_i32(&value, 42)
11
12 // Atomically load the value
13 loaded := atomics.load_i32(&value)
14 println('Loaded value: ${loaded}') // Output: 42
15
16 // Atomic add: returns the new value after addition
17 new_value := atomics.add_i32(&value, 10)
18 println('After add: ${new_value}') // Output: 52
19
20 // Atomic swap: returns the old value
21 old := atomics.swap_i32(&value, 100)
22 println('Old value: ${old}, new value: ${atomics.load_i32(&value)}') // Output: 52, 100
23}
24