Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:27:46+00:00 2026-06-02T15:27:46+00:00

I am writing intrusive shared pointer and I am using C++11 <atomic> facilities for

  • 0

I am writing intrusive shared pointer and I am using C++11 <atomic> facilities for reference counter. Here are the relevant fragments of my code:

//...
mutable std::atomic<unsigned> count;
//...

void
SharedObject::addReference() const
{
    std::atomic_fetch_add_explicit (&count, 1u,
        std::memory_order_consume);
}

void
SharedObject::removeReference() const
{
    bool destroy;

    destroy = std::atomic_fetch_sub_explicit (&count, 1u,
        std::memory_order_consume) == 1;

    if (destroy)
        delete this;
}

I have started with memory_order_acquire and memory_order_release first but then I convinced myself that memory_order_consume should be good enough. After further deliberation it seems to me that even memory_order_relaxed should work.

Now, the question is whether I can use memory_order_consume for the operations or could I use weaker ordering (memory_order_relaxed) or should I use stricter ordering?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:27:47+00:00Added an answer on June 2, 2026 at 3:27 pm
    void
    SharedObject::addReference() const
    {
        std::atomic_fetch_add_explicit (&count, 1u, std::memory_order_relaxed);
    }
    
    void
    SharedObject::removeReference() const
    {
        if ( std::atomic_fetch_sub_explicit (&count, 1u, std::memory_order_release) == 1 ) {
             std::atomic_thread_fence(boost::memory_order_acquire);
             delete this;
        }
    }
    

    You want to use atomic_thread_fence such that the delete is strictly after the fetch_sub. Reference

    Quote from the linked text:

    Increasing the reference counter can always be done with
    memory_order_relaxed: New references to an object can only be formed
    from an existing reference, and passing an existing reference from one
    thread to another must already provide any required synchronization.

    It is important to enforce any possible access to the object in one
    thread (through an existing reference) to happen before deleting the
    object in a different thread. This is achieved by a “release”
    operation after dropping a reference (any access to the object through
    this reference must obviously happened before), and an “acquire”
    operation before deleting the object.

    It would be possible to use memory_order_acq_rel for the fetch_sub
    operation, but this results in unneeded “acquire” operations when the
    reference counter does not yet reach zero and may impose a performance
    penalty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some android code in preparation for a graphics intensive app I
Writing documentation in html requires some code examples. What to do with characters that
Writing a python program, and I came up with this error while using the
Writing an asynchronous Ping using Raw Sockets in F#, to enable parallel requests using
While writing some C code, I decided to compile it to assembly and read
In general, can Mathematica automatically (i.e. without writing code specifically for this) exploit GPU
I'm writing very processor-intensive cryptography code (C#), so I'm looking for any performance gains,
I'm writing some pretty CPU-intensive, concurrent numerical code that will process large amounts of
I'm going to start writing a 3D game in unmanaged C++ using the DirectX
I'm writing a very AJAX-intensive application using Rails 3.0.4, and testing it with RSpec.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.