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

  • Home
  • SEARCH
  • 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 8800287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:32:55+00:00 2026-06-14T00:32:55+00:00

I would like to use a std::atomic<std::shared_ptr> in my code so that the shared_ptr

  • 0

I would like to use a std::atomic<std::shared_ptr> in my code so that the shared_ptr can be atomicaly updated, but I have a problem when accessing the shared_ptr. The load() method on the atomic seems to reduce the ref-count on the shared_ptr, so that I can’t actually use the object without it being deallocated.

Here is a simplified piece of code that shows the problem…

typedef shared_ptr<MyClass> MyClassPtr;
typedef atomic<MyClassPtr> MyClassAtomicPtr;

// 1.
MyClassPtr ptr( new MyClass() );
printf("1. use_count=%d\n", ptr.use_count());

// 2. 
MyClassAtomicPtr atomicPointer(ptr);
printf("2. use_count=%d\n", ptr.use_count());

// 3.
{
    MyClassPtr p = atomicPointer.load();
    printf("3a. use_count=%d\n", ptr.use_count());
}
printf("3b. use_count=%d\n", ptr.use_count());

// 4.
{
    MyClassPtr p = atomicPointer.load();
    printf("4a. use_count=%d\n", ptr.use_count());
}
printf("4b. use_count=%d\n", ptr.use_count());

The output of this is:

1. use_count=1
2. use_count=2
3a. use_count=2
3b. use_count=1
4a. use_count=1
4b. use_count=-572662307

I understand steps 1 and 2. But at step 3, I would expect the assignment to the shared_ptr to increase the ref-count to 3, and then when it goes out of scope for the ref-count to go back down to 2. But in fact it stays at 2 when assigned and then decreases to 1 when the shared_ptr goes out of scope. Similarly in step 4, where the ref-count goes to zero and the object is deleted.

So my question is: how can I access and use the shared_ptr managed by the atomic without destroying it?

(I was compiling with Visual Studio 2012 Version 11.0.50727.1 RTMREL)

  • 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-14T00:32:57+00:00Added an answer on June 14, 2026 at 12:32 am

    I believe the standard way to atomically load and store shared pointers are to use the functions in §20.7.2.5[util.smartptr.shared.atomic]. It seems only libc++ of clang support them:

    template<class T> bool atomic_is_lock_free(const shared_ptr<T>* p);
    template<class T> shared_ptr<T> atomic_load(const shared_ptr<T>* p);
    template<class T> shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
    template<class T> void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
    template<class T> void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
    template<class T> shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
    template<class T> shared_ptr<T> atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
    template<class T> bool atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
    template<class T> bool atomic_compare_exchange_strong(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
    template<class T> bool atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w, memory_order success, memory_order failure);
    template<class T> bool atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w, memory_order success, memory_order failure);
    

    So you code could be written as:

    auto ptr = std::make_shared<MyClass>();
    printf("1. use_count=%d\n", ptr.use_count());
    
    {
        auto p = std::atomic_load(&ptr);
        printf("3a. use_count=%d\n", ptr.use_count());
    }
    
    printf("3b. use_count=%d\n", ptr.use_count());
    
    {
        auto p = std::atomic_load(&ptr);
        printf("3a. use_count=%d\n", ptr.use_count());
    }
    
    printf("4b. use_count=%d\n", ptr.use_count());
    

    But I can’t find such supports listed on MSDN, so the best you could do is to use a mutex. (Actually, the implementation of these functions in libc++ uses a mutex too.)

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

Sidebar

Related Questions

I have class foo that contains a std::auto_ptr member that I would like to
So here is what I would like to do: I use std::pair , but
I have an array of Integers in Java, I would like use only a
I would like to use the logout function from Django but not sure how
I would like to use the MFMailComposeViewController mailComposeDelegate property with completion block syntax, but
I would like to copy the content of one std::map into another. Can I
In my project I use the std::queue class. I would like to know what
I am writing a numerical code, in which I would like to use a
I'd like to use the following idiom, that I think is non-standard. I have
I would like to use algorithm std::include to work with heterogeneous STL collections. In

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.