Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by kaba0 on Monday November 28, 2022 @06:42PM
(#33781538)
The compiler is free to never write out your changes to memory in that shared resource use-case, which you might be depending on. AFAIK rust doesn’t have a standardized memory model which should be needed to write correct code that data races.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by ameliaquining on Monday November 28, 2022 @09:26PM
(#33782927)
This isn't a difference between Rust and C++; data races are undefined behavior in both languages. If you want two different threads to be allowed to access the same memory without locking, that's what std::atomic (in C++) or std::sync::atomic (in Rust) does.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by cyber_kinetist on Tuesday November 29, 2022 @05:54AM
(#33785997)
He’s talking about formal memory models, which are a bit different from what you’re talking about (though you’re correct that they are pragmatically similar.)
C/C++ has a formalized memory model that describe the high level logical rules/guarantees for writing safe concurrent code, while Unsafe Rust doesn’t have one explicitly written in spec/paper (although they kinda borrowed a less-formalized version of it from C/C++.) It has to do with ironing out hardware-specific behavior to obey a certain set of rules, while making sure all the compiler optimizations do not perform any invalid transformations against these rules. (The Rustonomicon explains this well in layman terms, and acknowledges the complexity of the issue: https://doc.rust-lang.org/nomicon/atomics.html)
In practice, it seems like the C/C++ model has some glaring flaws anyway, so I can’t say that Rust’s is really “worse”. But since Rust has this mission to be better than C++ in terms of safety, this is one of thorny issues of Rust that need to be tackled to really let its advantages shine.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by JonChesterfield on Tuesday November 29, 2022 @08:30AM
(#33787306)
I thought the C11 memory model was considered correct, possibly modulo 'consume' which seems to be ignored. The implementation in terms of types instead of operations has a performance cost but otherwise works. What glaring flaws do you have in mind?
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by cyber_kinetist on Tuesday November 29, 2022 @12:10PM
(#33790221)
Well, not exactly “glaring” (unless you are a PL expert), but for instance there’s a paper that was referenced in that Rustonomicon link:
https://fzn.fr/readings/c11comp.pdf
And also
https://plv.mpi-sws.org/scfix/paper.pdf
Though maybe these issues were fixed in C++20 (which I only learned till recently that they’ve revised their memory model there!)
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by JonChesterfield on Tuesday November 29, 2022 @07:23PM
(#33795289)
Nice references. Going to take me a while to make sense of them. A superficial interpretation is one says relaxed doesn't work and the other says sequentially consistent doesn't work. Not great news tbh, hopefully it'll look better on closer examination.
Re:Const vs. constexpr vs. consteval vs. constinit in C++20
by eloff on Monday November 28, 2022 @10:33PM
(#33783389)
It's undefined behavior. You can write unsafe code that's UB in both Rust and C++ (it's permitted, which I think is your objection), but those are not correct programs and they may behave in surprising ways or break when seemingly unrelated changes are made.
Define 'permitteed'. Unsafe is, among other things, exactly that. It tells rustc to permit you to do things that are ... well, unsafe. Including the ones you listed.
Maybe you don't understand the meaning of unsafe in Rust?
Unsafe causing UB is a possibility. As is taking two &mut refs to the same memory location using exactly that keyword. And that example works for multi-threaded Rust code using a shared resource accessed as such too.