An obvious sort of solution is to put Y inside X. Rust's Mutex
is a container and by creating a Mutex<(X, Mutex<Y>)>
(for example) you guarantee that the inner mutex cannot be locked first and cannot be unlocked last.
This has the potential drawback that you cannot lock Y
by itself (without locking X
), even though doing so does not lead to deadlock if you don't lock X
. There might be something clever you can do with RwLock
here but that's just a stray thought. In general I think it will depend on why you need two mutexes in the first place.