Skip to main content

Practice ยท 2 of 2

Deadlock triage

CHALLENGE
Difficulty: advanced+25 XP

Implement static int SafeTransfer(Account a, Account b, int amount) that moves amount from a to b using Monitor.TryEnter with a timeout on both locks, acquired in a CONSISTENT ORDER (by account id) so it can never deadlock โ€” releasing cleanly on every path. Implement static void UnsafeTransfer(...) the naive way (lock a then lock b, in argument order) to demonstrate the ABBA deadlock the lesson catalogs. Return 1 on success, 0 on timeout in SafeTransfer.

Back to lesson: Practice: Concurrency drills