Referencing mutex content avoiding deadlock?

Is there a way to write it in single line?


What is your question?
Btw, your code has a deadlock.

1 Like

How to avoid dedlock, please?
Using only single line of code if possible.

I don't think it is possible to do it in a "single" line, unless you just put multiple lines in one line separated by semicolons (rustfmt won't allow it, but you can forcibly turn it off for that "line").
The issue is that the mutex is "locked" as long as the lock guard object is not dropped (the thing returned by mutex.lock().unwrap(). So if you call mutex.lock() twice in a line in a function call, the lock guard is not dropped and so you have a deadlock. You have to obtain the lock, extract whatever you want to extract, drop the lock (either by introducing a scope or by calling drop on the lock guard).
What is your real code problem? Maybe there is a different solution to what you want to do.

1 Like

Aha. Thanks for answer. I did think so.

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.