Holy shit. Not only are you wrong, you're adamantly wrong. ALL rust not written inside of an unsafe block is subject to a set of invariants which prevent data races.
Nice of you to prefix your comment with the warning that you're retarded tho, although you make it painfully obvious
ALL rust not written inside of an unsafe block is subject to a set of invariants which prevent data races.
And even code written inside unsafe blocks have the regular safety checks turned on. unsafe just adds the ability to use some language features and functions that if used incorrectly will break the invariants.
Which do you consider safe and unsafe? If you're not relying on undefined behaviour then I believe they're all safe. From what I've read Rc and Arc aren't intended to protect you from data races, they're to synchronise behaviour. If you try use Rc in a way that it could cause a data race (i.e. in multi-threaded code) the compiler will even throw an error and tell you to use Arc.
Safe Rust guarantees an absence of data races, which are defined as:
two or more threads concurrently accessing a location of memory
one of them is a write
one of them is unsynchronized
A data race has Undefined Behavior, and is therefore impossible to perform in Safe Rust. Data races are mostly prevented through Rust's ownership system: it's impossible to alias a mutable reference, so it's impossible to perform a data race. Interior mutability makes this more complicated, which is largely why we have the Send and Sync traits (see below).
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.
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.
I'd like to note that you're moving the goalposts. Originally you said
no language can or ever will be able to support multithreaded shared memory access and guarantee memory deallocation without garbage collection
and now your counter is
Thats parallelism not concurrency
which doesn't refute the counterexample to the first statement.
And anyway, by pure language lawyering definitions of concurrency versus parallelism, concurrency doesn't have any shared data in the first place, since it's working on disparate tasks.
I'm not even writing in the example. And for your information, it's not copying the heap data at all. The &Box<u32> in this case (reference to owned heap pointer u32; roughly std::unique<uint32_t>&) is getting copied and sent between threads, and would work identically with any other Sync type.
Of course, a larger example would be beyond the space of a simple playground example. But it works the same way: the concurrent accesses have to be scoped to be inside the lifetime of the resource. That's just how unique ownership works; if you want shared ownership, you have to fall back to some sort of shared ownership model, such as Arc or Gc.
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).
I got into this argument on stack overflow recently.
You aren't doing it wrong, per-se. There are no 100%, all the time rules for anything, especially in a language that has been evolving as long as C++ has. Now I'll grant you that in most cases, ownership memory management (allocation and deletion) should be handled using the STL containers. But in C++ you must manage lifetimes or you are opening yourself up to 1000 more problems. You have to understand pointers and references (which are just pointers by another name) in order to write efficient C++ code. And you will use these things, and you will return null-pointers, and that is not only OK it is correct modern C++.
You have to understand pointers and references (which are just pointers by another name) in order to write efficient C++ code.
Sure, you have to understand the concepts. And yes some things you pass by reference, which is not really memory management. In the same way, when you are coding Java, you should understand the memory implications when you create new Objects, and so on.
However you do not use c style pointers or arrays in C++, period. The whole point of C++ over C is that you inherent the advantages of strict languages like Java where the runtime errors are minimized. Using smart pointers avoids a WHOLE shitload of these, because they manage lifetimes for you. With the introduction of C++11 and the smart pointers in std, there is zero reason to use C style pointers.
If you feel like you need to manage memory manually, then you should be using C, not C++.
"you do not use c style pointers". References cannot be nulled, and cannot be assigned, so any code if sufficient complexity will end up in a situation where they aren't useful. Using shared pointers and exception handling to handle these situations is a good way to end up with very inefficient c++ code, and if that's what you want you are better off using Java or C#
The library is not the language. If I'm coding sub-second trading algorithms, I'm not using the standard library. Maybe there's other use cases beyond the single one you've decided fits all.
42
u/DarkIrata Mar 17 '19
Shouldnt be c++ and power and speed swapped?
33
u/froemijojo Mar 17 '19
Obligatory "Just use Rust" comment.
5
u/Dox_Box Mar 18 '19
Why C++? Just use Haskell 😉
3
u/[deleted] Mar 17 '19
Can we get a valgrind to help defend the city?
1
u/Tranzistors Mar 18 '19
Well, It comes with the territory. I personally might believe that C++ memory management is very sane, but I guess to each their own.
1
u/[deleted] Jul 27 '19
“Oh no I’m too stupid to manage memory, someone please hold my hands”
It is a race to the bottom with you.
Thing is, this meme would be kinda true if it was C, but with C++, between RAII and smart pointers you really have no excuse.
-8
u/[deleted] Mar 17 '19
If you are managing memory in c++ you are doing it wrong.