DEV Community

Cover image for rustc VS OS threads
Abdulrhman Elshikh
Abdulrhman Elshikh

Posted on

rustc VS OS threads

The Borrow Checker Requires Predictable Lifetimes

The borrow checker is a static analysis tool. It must be able to look at your code at compile time and definitively prove exactly when a reference starts, when it ends, and that no two threads overlap.

When you spin up multiple independent OS threads, the compiler cannot predict when they will execute.

Thread A might run for 5 milliseconds, get interrupted by the operating system, and then Thread B might run.

Because the execution order is determined at runtime by the OS scheduler, the compiler cannot statically guarantee that two threads won’t try to access the same memory address at the same fraction of a second.

Top comments (0)