4.9 Ordered Locking
The Ordered Locking Pattern is another way to ensure that deadlock cannot occur, this time by preventing condition 4 (circular waiting) from occurring. It does this by ordering the resources and requiring that they always be locked by a client in that specified order. If this is religiously enforced, then no circular waiting condition can ever occur.
The Ordered Locking Pattern eliminates deadlock by ordering resources and enforcing a policy in which resources must be allocated only in a specific order. For monadic (that is, functions that can be used independently of others) operations, the client can invoke them without special consideration and the called function will lock and release the resource internally. ...