Race Conditions & Data Races
redixhumayun.github.io·85w
Preview
Report Post

I’ve been using Rust at work for the last few months and keep hearing about “fearless concurrency” in Rust. I’m still not entirely sure what that means but I mistakenly assumed that it meant that race conditions were impossible in Rust. This is obviously wrong, but it took me a while to understand why.

Race Conditions In Rust

Here’s some code written in Rust that has a race condition in it because of application logic. It uses the classic example of tranferring money from one account to another but does it in an extremely silly way

struct Account {
balance: Mutex<i32>,
}

fn transfer2(amount: i32, account_from: Arc<Account>, account_to: Arc<Account>) -> &'static str {
// First atomic block
let bal;
{
let from_balance = account_from.balance.lock().unwrap();
bal = *from_balanc...

Similar Posts

Loading similar posts...