You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/rust-prevents-data-races-not-race-conditions/index.md
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,14 @@ This matters because a data race is [Undefined Behavior](https://doc.rust-lang.o
34
34
A data race does not mean you might read a "stale" value.
35
35
It means the compiler is allowed to do anything like tear a write in half and reorder it.
36
36
37
+
And you can't wave this away as a harmless race that happens to work out. As Raph Levien notes in [With undefined behavior, anything is possible](https://raphlinus.github.io/programming/rust/2018/08/17/undefined-behavior.html):
38
+
39
+
> It used to be thought that data races could be classified into "benign" and dangerous categories, but research strongly suggests that the former category doesn't exist.
40
+
41
+
In other words, every data race is a real bug!
42
+
And because it's Undefined Behavior, the symptom can show up far away from the cause and much later,
43
+
in the form of a corrupted value, a crash, or a security hole that only appears under heavy load.
44
+
37
45
For example, here are two threads incrementing the same counter:
38
46
39
47
```rust
@@ -112,10 +120,6 @@ The compiler enforces this through two marker traits, [`Send`](https://doc.rust-
112
120
113
121
That's the whole idea. Rust pushes the locking checks from runtime into the type system.
114
122
115
-
That guarantee holds up in production. Vivek Bagaria, whose team writes their entire robotics autonomy stack in Rust at [Matic](https://maticrobots.com/), put it this way in an [interview](https://filtra.io/rust/interviews/matic-apr-25):
116
-
117
-
> Rust is built for fearless concurrency, and it achieves it! Data races are basically impossible unless you are actively trying to shoot yourself in the foot.
118
-
119
123
{% info(title="Key takeaways") %}
120
124
121
125
- Synchronized access is not a data race, so it's allowed.
0 commit comments