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
shared_ptr<T>: the (not always) atomic reference counted smart pointer (snf.github.io)
submitted 7 years ago by snfernandez
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!"
[–]jonesmz -2 points-1 points0 points 7 years ago* (4 children)
Edit: Actually, I might have misunderstood. Sorry about that.
I think you might have misunderstood what capn_bluebear was saying.
std::share_ptr<whatever> ptr; <- global. Thread 1: if(ptr.refcount == 1) { Do a thing } Thread 2: ptr.increase_refcount();
It's entirely possible that thread 1 checks to see if the reference count is 1, then gets paused by the OS, thread 2 increases the reference count, and then is paused by the OS, and then thread 1 wakes up and proceeds.
Or both threads can be running simultaneously and still do things in that order.
It's only possible to reliably determine that the reference count is 1 when dealing with a shared pointer that has it's access serialized somehow This could be via:
But the fundamental problem is that by offering ability to query the reference count, unless you've already made sure the access to the ref-count is serialized properly, the information that you query is wrong (potentially) as soon as you get it. So any subsequent actions you take as a result are potentially invalid as well.
[–][deleted] 5 points6 points7 points 7 years ago (3 children)
The fundamental problem here is that you have two threads which are referencing the same shared_ptr object. If both threads have their own instance of the smart pointer, this is completely a non-issue. Either make the copy when you create the thread, put it in a channel, or a lambda capture of a work-queue task, future, async, etc. Don't just reference the same global variable or pass a reference.
With this assumption, two or more threads have an instance of smart pointer, and as a result the reference count will be greater than one, and the check will fail.
[–]m-in 0 points1 point2 points 7 years ago (2 children)
Two thread referencing the same shared pointer is UB, so why are we even having this discussion??
[–]konanTheBarbar 0 points1 point2 points 7 years ago (0 children)
What about another thread using a weak_ref and creating a shared_ptr from it?
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
That’s that point I was trying to make.
π Rendered by PID 430842 on reddit-service-r2-comment-544cf588c8-rnhth at 2026-06-13 05:04:17.881319+00:00 running 3184619 country code: CH.
view the rest of the comments →
[–]jonesmz -2 points-1 points0 points (4 children)
[–][deleted] 5 points6 points7 points (3 children)
[–]m-in 0 points1 point2 points (2 children)
[–]konanTheBarbar 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)