@@ -1695,6 +1695,40 @@ pub fn fence(order: Ordering) {
16951695///
16961696/// Panics if `order` is [`Relaxed`].
16971697///
1698+ /// # Examples
1699+ ///
1700+ /// Without `compiler_fence`, the `assert_eq!` in following code
1701+ /// is *not* guaranteed to succeed, despite everything happening in a single thread.
1702+ /// To see why, remember that the compiler is free to swap the stores to
1703+ /// `IMPORTANT_VARIABLE` and `IS_READ` since they are both
1704+ /// `Ordering::Relaxed`. If it does, and the signal handler is invoked right
1705+ /// after `IS_READY` is updated, then the signal handler will see
1706+ /// `IS_READY=1`, but `IMPORTANT_VARIABLE=0`.
1707+ /// Using a `compiler_fence` remedies this situation.
1708+ ///
1709+ /// ```
1710+ /// use std::sync::atomic::{AtomicBool, AtomicUsize};
1711+ /// use std::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT};
1712+ /// use std::sync::atomic::Ordering;
1713+ /// use std::sync::atomic::compiler_fence;
1714+ ///
1715+ /// static IMPORTANT_VARIABLE: AtomicUsize = ATOMIC_USIZE_INIT;
1716+ /// static IS_READY: AtomicBool = ATOMIC_BOOL_INIT;
1717+ ///
1718+ /// fn main() {
1719+ /// IMPORTANT_VARIABLE.store(42, Ordering::Relaxed);
1720+ /// // prevent earlier writes from being moved beyond this point
1721+ /// compiler_fence(Ordering::Release);
1722+ /// IS_READY.store(true, Ordering::Relaxed);
1723+ /// }
1724+ ///
1725+ /// fn signal_handler() {
1726+ /// if IS_READY.load(Ordering::Relaxed) {
1727+ /// assert_eq!(IMPORTANT_VARIABLE.load(Ordering::Relaxed), 42);
1728+ /// }
1729+ /// }
1730+ /// ```
1731+ ///
16981732/// [`fence`]: fn.fence.html
16991733/// [`Ordering`]: enum.Ordering.html
17001734/// [`Acquire`]: enum.Ordering.html#variant.Acquire
0 commit comments