Deadlock avoidance dynamically coordinates resource allocation so concurrent executions do not enter a deadlocked state. In a money-transfer service, an arbitrator grants all required accounts together; otherwise, competing transfers can hold one account while waiting for another. section 11.2.2
Account-transfer arbitrator
A Go Arbitrator tracks accounts in use with a map and protects that state with a condition variable. LockAccounts waits until every requested account is available, marks them all in use, and returns. Transfer then updates both balances and calls UnlockAccounts, which frees the accounts and broadcasts to suspended goroutines. section 11.2.2
Trace how a condition variable suspends goroutines when accounts are unavailable and resumes them after resources are released. section 11.2.2
Keep the practical limits in view:
- Avoidance algorithms such as the banker’s algorithm require advance knowledge of each execution’s maximum resource needs, which operating systems and runtimes generally lack. section 11.2.2
- They also assume a fixed set of executions, unlike realistic systems where processes start and terminate continuously. section 11.2.2
