...
The solution to the deadlock problem is to lock in predefined order in the deposit() function. In the following example, each thread will lock based on bank_account's id defined in the struct initialization. This way circular wait problem is avoided and when one thread requires a lock will guarantee it will require the next lock.
...