Why std mutex required twice by one thread not panic?

This is what Rust documentation describes Mutex::lock

Panics

This function might panic when called if the lock is already held by the current thread.

But actually running the following code will block the thread instead of panicking.

fn main(){
    println!("start");
    let mutex = std::sync::Mutex::new(10);
    let _a = mutex.lock().unwrap();

    // deadlock occurred here, why doesn't it panic?
    let _b = mutex.lock().unwrap(); 

    println!("we can never reach this");
}

Is that a bug?

Not a bug as it says "might" not "will.

Similar question has come up before:

1 Like

Thanks. I haven't found that topic before. :sweat_smile:

Two paragraphs above your link:

The exact behavior on locking a mutex in the thread which already holds the lock is left unspecified. However, this function will not return on the second call (it might panic or deadlock, for example).

AFAIK the panic section specifically is just there as a matter of documentation policy or the like.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.