top | item 39322155

(no title)

MaxRegret | 2 years ago

That's what C++ does because it has no way to ensure that you use the atomic reference counts in multi-threaded code. But, as the author writes in the blog post, Rust can in fact ensure this. So it lets you to use the more efficient non-atomic reference count for single-threaded use, saving the unnecessary cost of various memory access barriers.

Just because a language is designed for concurrent programming, it shouldn't make it impossible to achieve full single-threaded performance, as long as you're not compromising safety.

discuss

order

PaulDavisThe1st|2 years ago

this is a bit weird.

if you have only 1 thread, you don't atomic, and thus not using atomic reference counts is fine

but if you have more than 1 thread, you can't use a non-atomic refcount, so you can't use Rc but must use Arc.

"but that's such a simple change, just change the decl with a 1 char addition! Pluse, Rust won't let you do bad stuff if you've forgotten to change the type".

I guess I'm just old. Old enough that I've already implemented all the data structures and methods I need in C++, including safely passing around shared_ptr<T>.

Rusky|2 years ago

In Rust, you may have multiple threads yet still use non-atomic reference counts for objects that are never shared between multiple threads.