Why would I std::move an std::shared_ptr?

I think that the one thing the other answers did not emphasize enough is the point of speed. std::shared_ptr reference count is atomic. increasing or decreasing the reference count requires atomic increment or decrement. This is hundred times slower than non-atomic increment/decrement, not to mention that if we increment and decrement the same counter we … Read more

How do shared pointers work?

Basically, shared_ptr has two pointers: a pointer to the shared object and a pointer to a struct containing two reference counts: one for “strong references,” or references that have ownership, and one for “weak references,” or references that don’t have ownership. When you copy a shared_ptr, the copy constructor increments the strong reference count. When … Read more

Is make_shared really more efficient than new?

As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within XCode4. Well that appears to be your problem. The C++11 standard states the following requirements for make_shared<T> (and allocate_shared<T>), in section 20.7.2.2.6: Requires: The expression ::new (pv) T(std::forward(args)…), where pv has type void* and points to storage suitable to hold … Read more

shared_from_this causing bad_weak_ptr

John Zwinck’s essential analysis is spot on: The bug is that you’re using shared_from_this() on an object which has no shared_ptr pointing to it. This violates a precondition of shared_from_this(), namely that at least one shared_ptr must already have been created (and still exist) pointing to this. However, his advice seems completely beside the point … Read more

Should I pass a shared_ptr by reference? [duplicate]

In controlled circumstances you can pass the shared pointer by constant reference. Be sure that nobody is concurrently deleting the object, though this shouldn’t be too hard if you’re careful about to whom you give references. In general, you should pass the shared pointer as a straight copy. This gives it its intended semantics: Every … Read more

std::shared_ptr thread safety explained

As others have pointed out, you’ve got it figured out correctly regarding your original 3 questions. But the ending part of your edit Calling reset() in thread IV will delete previous instance of A class created in first thread and replace it with new instance? Moreover after calling reset() in IV thread other threads will … Read more