4 ms·
A data race occurs when two or more parallel processes access the same data and at least one of them is mutating it. Safe Rust forbids this through ownership a
by duped 4y ago
A data race occurs when two or more parallel processes access the same data and at least one of them is mutating it.
Safe Rust forbids this through ownership and move semantics along with lifetime analysis. You may create any number of immutable aliases to data, or exactly one mutable alias. You cannot mix the two.
Unsafe allows you to (kind of) sidestep the automatic checking, but its your responsibility to uphold those conditions.
- commandlinefan 4y agoSounds more or less (and pardon me if I'm oversimplifying) Java's "synchronized" keyword that most people stay away from just because it slows the code down so much you don't get the benefit of parallelization.
- duped 4y agoThey couldn't be more different. Java's synchronized keyword changes how code is interpreted to use expensive locking and data fences in order to prevent logical race conditions. Rust's semantics do not alter the compiled code or insert any locking behavior. They prevent you from writing incorrect code to begin with. In other words, Rust doesn't do anything magical to deal with data races at run time. It prevents you from expressing programs that have data races in the first place. It's a compile time analysis, not a run time behavior.
- commandlinefan 4y agoInteresting, thanks for clarifying.