Search code examples
javamultithreadingconcurrencylocking

Safe acquisition of all striped lock at once


Let's say I've implemented a striped lock to safely but efficiently access some data structure, but that structure has a method that requires all locks to be acquired at the same time. What is the most correct way to do this?


Solution

  • If you have to acquire multiple locks at the same time, the only way to safely do that (i.e. avoid potential deadlock), is to always acquire the locks in the same order. as long as you can come up with a consistent order for the locks and always acquire them in that order (and release them in the reverse order), it should work without deadlock.