Tokio tutorial MutexGuard contention

⚓ Rust    📅 2025-08-18    👤 surdeus    👁️ 6      

surdeus

I'm going through the Tokio tutorial. I have this code example:

    Set(cmd) => {
        let mut db = db.lock().unwrap();
        db.insert(cmd.key().to_string(), cmd.value().clone());
        // Is the critical section active while we're constructing the Simple?
        Frame::Simple("OK".to_string())
    }

Which uses a blocking MutexGuard in an inner scope for mutating the db. Wouldn't the critical section be shorter if we do:

    Set(cmd) => {
        db.lock().unwrap().insert(cmd.key().to_string(), cmd.value().clone()); // Did CS end here?
        Frame::Simple("OK".to_string())
    }

Thanks.

5 posts - 2 participants

Read full topic

🏷️ Rust_feed