It must be a deadlock kinda day. Pete McKinstry points to a Java deadlock avoidance scheme which involves knowing and using a total ordering of the locks that you wish to acquire. This is similar to Andrei Alexandrescu’s C++ idea of always acquiring multiple locks in increasing memory address order.
Both of these are fine if you can get at all of the locks from one place. I find that that’s rarely the case and more often the locks are within objects and I don’t want to break the encapsulation to expose the need to lock around a method so that I can grab the lock that’s inside of the object and then use it from outside of the object in some multiple lock, deadlock avoidance, total ordering, code based lock scheme… In my opinion these ideas are all well and good in theory but just don’t tend to work in practice if you are working with properly factored code….
Updated 5th May 2023 to fix broken links