|
138 | 138 | //! |
139 | 139 | //! In general, *all* atomic accesses on read-only memory are Undefined Behavior. For instance, attempting |
140 | 140 | //! to do a `compare_exchange` that will definitely fail (making it conceptually a read-only |
141 | | -//! operation) can still cause a page fault if the underlying memory page is mapped read-only. Since |
| 141 | +//! operation) can still cause a segmentation fault if the underlying memory page is mapped read-only. Since |
142 | 142 | //! atomic `load`s might be implemented using compare-exchange operations, even a `load` can fault |
143 | 143 | //! on read-only memory. |
144 | 144 | //! |
|
181 | 181 | //! let spinlock = Arc::new(AtomicUsize::new(1)); |
182 | 182 | //! |
183 | 183 | //! let spinlock_clone = Arc::clone(&spinlock); |
| 184 | +//! |
184 | 185 | //! let thread = thread::spawn(move|| { |
185 | | -//! spinlock_clone.store(0, Ordering::SeqCst); |
| 186 | +//! spinlock_clone.store(0, Ordering::Release); |
186 | 187 | //! }); |
187 | 188 | //! |
188 | 189 | //! // Wait for the other thread to release the lock |
189 | | -//! while spinlock.load(Ordering::SeqCst) != 0 { |
| 190 | +//! while spinlock.load(Ordering::Acquire) != 0 { |
190 | 191 | //! hint::spin_loop(); |
191 | 192 | //! } |
192 | 193 | //! |
|
203 | 204 | //! |
204 | 205 | //! static GLOBAL_THREAD_COUNT: AtomicUsize = AtomicUsize::new(0); |
205 | 206 | //! |
206 | | -//! let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst); |
| 207 | +//! // Note that Relaxed ordering doesn't synchronize anything |
| 208 | +//! // except the global thread counter itself. |
| 209 | +//! let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::Relaxed); |
| 210 | +//! // Note that this number may not be true at the moment of printing |
| 211 | +//! // because some other thread may have changed static value already. |
207 | 212 | //! println!("live threads: {}", old_thread_count + 1); |
208 | 213 | //! ``` |
209 | 214 |
|
|
0 commit comments