-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Atomic locking of multiple sync::mutex::Mutex #16405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Why is locking each mutex individually insufficient? |
Sequentially locking can cause deadlock with two threads:
|
extern crate sync;
use sync::mutex::Mutex;
struct Node {
mutex : Mutex
}
struct Link {
to : &Node,
from : &Node,
}
fn deadlock(link : &mut Link) {
let to_guard = link.to.mutex.lock();
let from_guard = link.from.mutex.lock(); // Deadlock can occur here
drop(to_guard);
drop(from_guard);
} |
The simplest solution to the problem you gave is to just lock the mutexes in address order. You could write a series of functions or methods on tuples of mutexes to do this. Example for pairs: http://is.gd/aNnVl5 This of course requires the order to be followed everywhere in the code, which can be difficult to verify. |
I'm working on a try-and-back-off method, based on libc++. I currently have locking for two mutexes, and am going to try generalizing it using macros tomorrow. |
I feel like I've gotten close, but can't quite seem to get the macro functioning as I want it to: http://is.gd/EfLEIV |
Since |
…lnicola fix: Include `for` construct in convert to guarded return conditions
There's currently no way of locking on two or more
sync::mutex::Mutex
at once.C++11 provides a
std::lock
vsstd::mutex::lock
, where the first is a variadic template that can lock multiple mutexes, and the second is a member method to lock a single mutex.Maybe a
sync::mutex::lock
would work?This might apply to the
std
wrapper Mutex types too.The text was updated successfully, but these errors were encountered: