You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/getting-started/readme.md
+37-6Lines changed: 37 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,8 @@ $ bundle add io-buffer-atomic
14
14
15
15
`io-buffer-atomic` extends `IO::Buffer` with atomic operations that are safe for concurrent access across threads and processes. When multiple threads or processes share memory (via `IO::Buffer`), you need atomic operations to prevent race conditions and ensure data consistency.
16
16
17
+
**Important**: Atomic operations work on raw memory bytes in host endian (native byte order). Both `:u32` and `:U32` map to the same atomic operation since they operate on the same raw memory bytes. The endianness distinction only affects how Ruby's `get_value`/`set_value` interpret bytes, not atomic operations.
18
+
17
19
Use atomic operations when you need:
18
20
-**Thread-safe counters**: Multiple threads updating shared counters without locks.
19
21
-**Process-safe coordination**: Multiple processes coordinating via shared memory.
@@ -77,6 +79,38 @@ result = buffer.atomic_xor(:u32, 0, 0b1111)
77
79
# => 0b0000
78
80
```
79
81
82
+
### Atomic Load
83
+
84
+
Atomic load operations ensure you read a complete, consistent value from shared memory, preventing torn reads:
85
+
86
+
```ruby
87
+
# Set a value:
88
+
buffer.set_value(:u32, 0, 42)
89
+
90
+
# Atomically read the value (ensures no torn reads):
91
+
result = buffer.atomic_load(:u32, 0)
92
+
# => 42
93
+
```
94
+
95
+
Use `atomic_load` instead of `get_value` when reading values that may be modified by other threads or processes, as it provides memory ordering guarantees and prevents data races.
96
+
97
+
### Atomic Store
98
+
99
+
Atomic store operations ensure you write a complete value atomically to shared memory:
100
+
101
+
```ruby
102
+
# Atomically write a value:
103
+
buffer.atomic_store(:u32, 0, 42)
104
+
105
+
# Read it back:
106
+
result = buffer.atomic_load(:u32, 0)
107
+
# => 42
108
+
```
109
+
110
+
Use `atomic_store` instead of `set_value` when writing values that may be read by other threads or processes, as it provides memory ordering guarantees and prevents torn writes.
111
+
112
+
**Note**: `atomic_load` and `atomic_store` work on raw bytes in host endian, matching the atomic operations themselves. Both `:u32` and `:U32` are equivalent for atomic operations.
113
+
80
114
### Compare and Swap
81
115
82
116
Compare-and-swap operations enable lock-free algorithms and optimistic concurrency:
@@ -88,7 +122,7 @@ buffer.set_value(:u32, 0, 10)
88
122
# Atomically swap if current value matches expected:
0 commit comments