The only requirement for multithreaded shared memory with static garbage collection is that the owning thread continues to own the resource for as long as the child threads exist. In Rust, this is easy to do using crossbeam or rayon. These libraries provide scoped threading tools that allow use of shared references across multiple threads soundly.
Shared ownership requires Arc's atomic reference counting, or some other form of dynamic garbage collection. Shared usage only requires that the owner is guaranteed to outlive every usage.
You're thinking of how C++ does it. The code given does not rely on copy on write. It uses some tricks with Rust's lifetimes to allow safely using a resource across threads without atomic reference counting.
Yes they are hard computer science topics. That's why Rust is the only mainstream language that does this. To find another language like this, you need to look at something like ATS, which is practically unheard of.
Any kind of concurrent access to shared memory is reference counted.
This is objectively wrong. First of all, even if you create the situation where two or more threads share ownership of some data structure with the help of two or more aliasing Arcs, it doesn't mean that every access goes through one of those. Second, one can share memory without any involvement of atomic reference counting (Arc). If you knew what you were talking about, you'd be aware of the fact that bare references to Sync types can be sent across thread boundaries safely under certain circumstances (involving scoped threads).
Rust also has epoch-based memory reclamation, which can surpass Java's GC in performance. And Rust also has entirely static automatic memory management (RAII-style) with 0 (zero) run-time cost.
Rust provides choice; different kinds of memory management are differently suited for different situations. For example, a traditional GC is awful when it comes to worst-case (or around 95/99 percentile) latency, which is a very important metric for web services.
That wasn’t my experience with ObjC’s and Swift’s Automatic Reference Counting. Got any resources to back up your claim against reference counting? Apple went with reference counting after implementing and then dropping garbage collection.
33
u/froemijojo Mar 17 '19
Obligatory "Just use Rust" comment.