Confusion with this Acquire fence

⚓ Rust    📅 2026-06-19    👤 surdeus    👁️ 1      

surdeus

Someone also asked this in rust lang discord server but it was a bit unclear to me...

Can someone please explain how this fence helps to make sure that nothing is accessing the shared allocation? It's a bit confusing because with Release and Acquire ordering one should be loading the Released value to make sure that an happens-before relationship was established...

Does this fence apply Acquire ordering to the fetching part of fetch_sub when the counter goes from 0 to 1?

impl<T> Drop for Arc<T> {
    fn drop(&mut self) {
        if self.data().ref_count.fetch_sub(1, Release) == 1 {
            fence(Acquire);
            unsafe {
                drop(Box::from_raw(self.ptr.as_ptr()));
            }
        }
    }
}

this piece of code is from the atomics and locks book: Rust Atomics and Locks — Chapter 6. Building Our Own "Arc"

5 posts - 3 participants

Read full topic

🏷️ Rust_feed