News for hackers. Stuff that matters.
Re:Java is not a safe language by nyc_pizzadev on Monday June 13, 2022 @07:05AM (#31723727)
How does Rust prevent data races?
Re:Java is not a safe language by DougBTX on Monday June 13, 2022 @07:09AM (#31723755)
> 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.

https://doc.rust-lang.org/nomicon/races.html

Re:Java is not a safe language by Sankozi on Monday June 13, 2022 @09:18AM (#31725126)
Definition of data race on that page is very limited.

Also on the same page "Data races are mostly prevented through Rust's ownership system".

Re:Java is not a safe language by kaba0 on Monday June 13, 2022 @05:32PM (#31732051)
Rust does prevent data races (as in multiple threads writing to the same memory location is explicitly forbidden by the borrow checker), but it doesn’t prevent race conditions, which is a more general family of concurrency bugs.

In short, the borrow checker allows a single owner at all times, or it allows references to the object, abiding these rules: it may have any number of read-only references (they are safe to share), OR a single read-write reference.

Re:Java is not a safe language by duped on Monday June 13, 2022 @10:10AM (#31725825)
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.

Re:Java is not a safe language by commandlinefan on Monday June 13, 2022 @01:00PM (#31728243)
Sounds 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.
Re:Java is not a safe language by duped on Monday June 13, 2022 @01:43PM (#31728830)
They 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.

Re:Java is not a safe language by commandlinefan on Monday June 13, 2022 @02:19PM (#31729299)
Interesting, thanks for clarifying.
Re:Java is not a safe language by gnulinux on Monday June 13, 2022 @12:30PM (#31727787)
Rust type system has primitive traits Send and Sync that formalize behavior expected for safe multithreaded concurrency. This way compiler can statically analyze the program and determine if it's safe.