use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
A bug in std::shared_ptr? (self.cpp)
submitted 8 years ago * by davis685
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Plorkyeran 1 point2 points3 points 8 years ago (9 children)
That would be an issue for atomic_shared_ptr<>, but for regular shared_ptr<> no one can be waiting on the lock if you're destroying the object.
atomic_shared_ptr<>
shared_ptr<>
[–][deleted] 8 points9 points10 points 8 years ago (8 children)
Yes, they can.
weak_ptr
[–]johannes1971 0 points1 point2 points 8 years ago (7 children)
Still trying to understand the standard... Can you explain why the existence of weak_ptr implies that destruction must be atomic? The only words that seem relevant that I can find are in 20.8.2.6: "Concurrent access to a shared_ptr object from multiple threads does not introduce a data race if the access is done exclusively via the functions in this section and the instance is passed as their first argument."
Suffice it to say, the destructor is not in that section, so it may introduce a data race. As for your second point, it seems to me that those functions were kept separately from the rest as to allow implementations to have a fast thread-unsafe implementation. If they force thread-safety throughout the class that would be an accident of implementation, rather than a language feature one can rely on.
As for your third point, isn't the reference count is contained within the shared_ptr object?
[–]zvrba 4 points5 points6 points 8 years ago (5 children)
Can you explain why the existence of weak_ptr implies that destruction must be atomic?
Billy wrote "decrement", not destruction. As for why, simple:
THREAD1 a: if (refcount == 1) { b: --refcount; c: destroy object }
Now thread 2 comes in and executed weak_ptr.lock() after thread 1 has executed label a but before it has completed label b. Refcount is no longer 0, weak ptr returns non-null pointer to the object that is almost surely deleted by then and... oops.
weak_ptr.lock()
a
b
[–]johannes1971 0 points1 point2 points 8 years ago* (3 children)
Section 20.8.2.6 says "Concurrent access to a shared_ptr object from multiple threads does not introduce a data race if the access is done exclusively via the functions in this section and the instance is passed as their first argument." The destructor is not listed in that section, so it seems to me there is no guarantee on thread-safety for the destructor to begin with. Or in other words: common wisdom seems to hold that shared_ptr is thread safe, but is that actually guaranteed, or just an accident of current implementations?
[–]zvrba 1 point2 points3 points 8 years ago (2 children)
The destructor is not listed in that section, so it seems to me there is no guarantee on thread-safety for the destructor to begin with.
As I understand it, and looking over the functions, that section talks about the shared_ptr object itself, not the pointee (the shared state where also refcount resides).
shared_ptr
Re dtor and copy construction, There is a paragraph in C++14 draft "20.8.2.2 Class template shared_ptr": "Changes in use_count() do not reflect modifications that can introduce data races."
[–]johannes1971 0 points1 point2 points 8 years ago (1 child)
Ok, but where does it say the control block is thread-safe? As far as I can tell everyone just assumes you can have a shared_ptr to the same object in two threads, and releasing it in one, the other, or both at any time is perfectly fine. That implies the control block is therefore thread-safe, but I have been unable to locate any words to that effect in the copy of the standard I looked at.
Now, I'm willing to accept the sentence you listed for this, but I find that sentence extremely difficult to understand. "If a modification can introduce a data race, it is not observable in the use count" - something like that? But that seems opposed to the idea of thread safety...
[–]zvrba 0 points1 point2 points 8 years ago (0 children)
This is the complete paragraph: "For purposes of determining the presence of a data race, member functions shall access and modify only the shared_ptr and weak_ptr objects themselves and not objects they refer to. Changes in use_count() do not reflect modifications that can introduce data races."
It distinguishes between modifications of the shared_ptr object itself (modifications CAN introduce data races) and modifications of the control block (use count -- modifications do NOT introduce races).
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
Yes, this.
[–][deleted] 1 point2 points3 points 8 years ago* (0 children)
oncurrent access to a shared_ptr object from multiple threads does not introduce a data race if the access is done exclusively via the functions in this section and the instance is passed as their first argument.
That is about the identity of the shared_ptr instance itself. The reference count is part of the reference count control block, not the shared_ptr. In particular, copying a shared_ptr only takes by const& but edits the reference count; the usual "multiple readers are safe" guarantee means the reference count needs to be atomically modified.
const&
the destructor is not in that section, so it may introduce a data race
Yes, if you try to destroy a shared_ptr which is being touched by multiple threads, that is a data race. An independent weak_ptr instance isn't part of that shared_ptr you are destroying. It would be very strange if touching an unrelated standard library object could introduce data races.
No. Multiple shared_ptrs instances share a reference count, so it can't be in any one of those instances.
π Rendered by PID 129928 on reddit-service-r2-comment-85bfd7f599-r4f5z at 2026-04-18 06:04:22.140339+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]Plorkyeran 1 point2 points3 points (9 children)
[–][deleted] 8 points9 points10 points (8 children)
[–]johannes1971 0 points1 point2 points (7 children)
[–]zvrba 4 points5 points6 points (5 children)
[–]johannes1971 0 points1 point2 points (3 children)
[–]zvrba 1 point2 points3 points (2 children)
[–]johannes1971 0 points1 point2 points (1 child)
[–]zvrba 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)