liveBook · Manning

Deadlock avoidance

As explained in Learn Concurrent Programming with Go

Deadlock avoidance is the dynamic allocation of resources or scheduling of executions so that the system does not enter a deadlocked state.

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

Figure 11.12 Using a condition variable to suspend goroutines when accounts are unavailable

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

How Learn Concurrent Programming with Go covers it

How deep it goes
Works through condition-variable locking, waiting, broadcasting, and transfer integration, then stops short of runtime-wide avoidance.
Examples
Uses a Go bank-account transfer, an arbitrator, account-availability maps, and suspended transfer goroutines.
What it assumes
Assumes goroutines, mutexes, condition variables, and shared account state.
What you can do after
Builds a Go arbitrator that waits for complete resource availability and resumes blocked transfers after release.
How it connects
Connects condition variables, goroutines, resource allocation, banker’s algorithm, and deadlock avoidance.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.