Why do reference counts require both a release and acquire operation on the decrement?

Given a reference count decrement such as the following:

void release_and_maybe_deallocate(std::atomic<int>& count, foo* data)
{
    if (count.fetch_sub(1, std::memory_order_acq_rel) == 1)
    {
        delete data;
    }
}

It’s usually given as common knowledge that while the increment can use std::memory_order_relaxed (because the reference count must already be at least 1 because the reference count must’ve been acquired from somewhere else that was already holding a reference when it became visible to the current thread), while the decrement has to be both an acquire and a release operation. My question is why exactly the acquire part is nessecary, at least during the decrement.

I would imagine that the above could also written as:

void release_and_maybe_deallocate(std::atomic<int>& count, foo* data)
{
    if (count.fetch_sub(1, std::memory_order_release) == 1)
    {
        std::atomic_thread_fence(std::memory_order_acquire);
        delete data;
    }
}

Which would save a fence on the “happy” path that someone else is still holding a reference. My first thought as to why this might not work was that potentially an acquire might be needed because the sub needs to synchronise with all the previous increments, but I don’t think that’s correct, because the increments are all relaxed, so there’s no garantee that any increments could be visible on the other thread. In other words, this series of events might happen:

  1. Thread A creates a refcount with value 1
  2. Thread A publishes a pointer to the refcount and makes it visible with value 1 to Thread B
  3. Thread B does a relaxed increment on the refcount, which isn’t made visible to Thread A
  4. Thread A decrements the refcount, but because the increment above hasn’t yet been released, it still appears as 1 after an acquire and therefore Thread A deallocates the memory
  5. Thread B accesses the memory and causes a bug

So I would assume that the act of accessing the memory to decrement it is somehow sufficent to observe the relaxed increments on thread B, otherwise the above may happen and even acq_rel wouldn’t be sufficent for decrementing the refcount.

Another reason I thought of might be because we would want to avoid writes which happen after the decrement to avoid moving behind the decrement, but I’m also uncertain about that because surely any of those writes would have to be known to be to be to an address not being deallocated if the branch is taken, otherwise the program would still be wrong in a single threaded context (accesses which would be incorrect if the branch would be taken would happening before the branch, which obviously can happen in hardware but would be invalidated by the branch misprediction and never become visible to other threads)

I get that a release is nessecary to make sure that when the memory is deallocated the changes to the data from the current thread are visible to the deallocating thread (since the changes may effect the behaviour of the destructor) and that an acquire is nessecary on the deallocating thread to receive those changes, but I can’t think of a reason why the acquire is nessecary on the path where the memory isn’t deallocated.

Is moving the acquire to a fence only on the branch where the memory is deallocated still correct and combining the acquire and release into the increment just an arbitrary choice that everyone made? Or is there some aspect of the memory model I’ve misunderstood that makes the acquire nessecary before the decrement? Or perhaps there’s something about having the acquire be on a fence instead of an instruction that makes it either incorrect or has too much of a performance penalty when the branch is taken, although I don’t think the differences between an instruction acquire vs a fence acquire (which I know are slightly different) are that harsh

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật