Deadlock Solutions: Lock Ordering
You simply need to place a total order on the accounts and always lock the accounts in that order.

You define a &lt; operator on Accounts and then change the beginning like this:

mutex lock1 = getlock(from)
mutex lock2 = getlock(to)
if (from &lt; to)
    mutex lockSwap = lock1
    lock1 = lock2
    lock2 = lockSwap


It doesn't matter what order you choose, it just has to stay the same through the whole program.  In C, and other low level languages it is particularly easy to make this kind of &lt; operator because you can just convert the addresses of the objects to integers and do the &lt; on the integers.