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