TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Too dangerous for C++

93 pointsby dureuillover 1 year ago

11 comments

cjensenover 1 year ago
The two criticism at the end are... odd.<p>First, there is criticism that assigning to a shared_ptr is not synchronized so it would be bad to share a single shared_ptr object between threads. True, but that is no different than literally every other non-atomic object in C++. It&#x27;s not surprising in any way.<p>Second, there is criticism that assigning to the object pointed at by the shared_ptr is not synchronized between threads. This is odd because that&#x27;s not actually different than a single thread where there are two shared_ptrs pointing to the same object. That is, even with single threading you have a problem you must be careful about.
评论 #39323153 未加载
评论 #39324815 未加载
PaulDavisThe1stover 1 year ago
See also:<p><a href="https:&#x2F;&#x2F;www.boost.org&#x2F;doc&#x2F;libs&#x2F;1_65_0&#x2F;libs&#x2F;smart_ptr&#x2F;doc&#x2F;html&#x2F;smart_ptr.html#local_shared_ptr" rel="nofollow">https:&#x2F;&#x2F;www.boost.org&#x2F;doc&#x2F;libs&#x2F;1_65_0&#x2F;libs&#x2F;smart_ptr&#x2F;doc&#x2F;htm...</a><p>i.e. a single-threaded non-atomic shared_ptr<p>Rust fans can dislike on the &quot;C++ has no central library system like crates&quot; all they want, but there&#x27;s not many things you actually need when programming that don&#x27;t exist for C++, even if you don&#x27;t like them not coming in a little box that looks like other little boxes.
评论 #39324834 未加载
评论 #39323468 未加载
评论 #39323134 未加载
ok123456over 1 year ago
&gt; Apparently, this is enough of an issue that C++20 added a partial template specialization to std::atomic&lt;std::shared_ptr&gt;. My advice, though, would be &quot;don&#x27;t do that!&quot;. Instead, keep your shared pointer in a single thread, and send copies to other threads as needed.<p>This is to support an atomic lock-free shared_ptr. You can then use this as a building block for building lock-free data structures.
评论 #39324885 未加载
kevr2d2over 1 year ago
Pretty clickbaitey title.<p>It&#x27;s possible to implement in C++... so it&#x27;s not &quot;too dangerous&quot; for C++. It&#x27;s dangerous for people who don&#x27;t have knowledge of what they&#x27;re doing in C++; same as in any programming language.
评论 #39322174 未加载
评论 #39322321 未加载
评论 #39322370 未加载
reflexeover 1 year ago
From my experience, the biggest footgun with shared_ptr and multi threading is actually destruction.<p>It is very hard to understand which thread will call the destructor (which is by definition a non-thread-safe operation), and whether a lambda is currently holding a reference to the object, or its members. Different runs result different threads calling the destructor, which is very painful to predict and debug.<p>I think that rust suffers from the same issue, but maybe it is less relevant as it is a lot harder to cause thread safety issues there.
评论 #39324804 未加载
PaulDavisThe1stover 1 year ago
From the stackoverflow link within TFA:<p>&gt; With GCC when your program doesn&#x27;t use multiple threads shared_ptr doesn&#x27;t use atomic ops for the refcount. This is done by updating the reference counts via wrapper functions that detect whether the program is multithreaded (on GNU&#x2F;Linux this is done by checking a special variable in Glibc that says if the program is single-threaded[1]) and dispatch to atomic or non-atomic operations accordingly.<p>&gt; I realised many years ago that because GCC&#x27;s shared_ptr&lt;T&gt; is implemented in terms of a __shared_ptr&lt;T, _LockPolicy&gt; base class, it&#x27;s possible to use the base class with the single-threaded locking policy even in multithreaded code, by explicitly using __shared_ptr&lt;T, __gnu_cxx::_S_single&gt;. You can use an alias template like this to define a shared pointer type that is not thread-safe, but is slightly faster[2]:
评论 #39324862 未加载
flohofwoeover 1 year ago
Refcounted memory management on a large scale is slow anyway, with or without atomic refcounting. The bigger problem is that Rc, Arc or shared_ptr often only manage one small object, and that object lives in a separate tiny heap allocation. So you end up with many tiny heap allocations spread more or less randomly around in memory and the likelyhood of getting cache misses on access is much highter than tightly packing the underlying data into arrays and walking over the array items in order.<p>And if you only have a small number of refcounted references in your program, the small performance difference between atomic and non-atomic refcounting doesn&#x27;t matter either.<p>Same problem with Box and unique_ptr btw, a handful is ok, but once that number grows into the thousands all over the codebase it&#x27;s hard to do any meaningful optimization (or even figure out how much performance you&#x27;re actually losing to cache misses because it&#x27;s a death-by-a-thousand-cuts scenario).
bsdpufferfishover 1 year ago
Shared objects interacting across threads is a bad idea. Java did it “safely” forever ago, it just throws a lock on everything
评论 #39323777 未加载
vasilipupkinover 1 year ago
I am not going to be surprised to be downvoted, but you don&#x27;t need shared_ptr in C++, that is itself overkill<p>The point of C++ is performance. If you don&#x27;t need performance, why not just use Java or Python, why use Rust?
评论 #39322588 未加载
评论 #39322725 未加载
评论 #39322781 未加载
评论 #39322666 未加载
评论 #39322619 未加载
评论 #39323804 未加载
stathibusover 1 year ago
There are valid selling points to rust&#x27;s safety features, but this just feels like &quot;I use rust because I need my compiler to be my training wheels&quot;. More of a self-own than anything.
评论 #39323775 未加载
评论 #39323626 未加载
评论 #39323401 未加载
评论 #39323894 未加载
评论 #39323960 未加载
评论 #39323915 未加载
评论 #39323679 未加载
kazinatorover 1 year ago
&gt; <i>the Rc type does not support being sent between threads</i><p>So why even have such a thing in a language designed for concurrent programming from the ground up?<p>Arc should be called Rc, and that&#x27;s it.
评论 #39322155 未加载
评论 #39322129 未加载
评论 #39322128 未加载
评论 #39322192 未加载
评论 #39322141 未加载