Impose a consistent global lock ordering and always acquire locks in that order everywhere in the codebase, this eliminates the circular-wait condition, which makes the whole class of cyclic deadlocks impossible. Concretely, you define a hierarchy (lock1 before lock2 before lock3, by some documented rule), and every code path that needs multiple locks takes them in that ascending order; since no task ever holds a higher-ordered lock while waiting for a lower-ordered one, you can't form the cycle where A holds lock1 waiting for lock2 while B holds lock2 waiting for lock1. It costs nothing at runtime, is simple to reason about, and is enforceable, some teams number their locks and assert that acquisitions are in increasing order, or use static analysis to catch violations. The complementary practice is to minimize nested locking in the first place: the simplest deadlock-proofing is to rarely or never hold two locks at once, by keeping critical sections small and single-resource, so the question of ordering rarely even arises. Timeouts are a useful recovery net but are secondary, they turn a deadlock into a recoverable error rather than preventing it, and they add livelock risk, so the primary engineering answer is "consistent lock ordering plus avoid nesting locks."
RTOS & Real-Time Concepts · Interview question
What's the most practical way to prevent deadlock in an RTOS application?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Deadlocks & How to Avoid Them
Two tasks each holding a lock the other needs, frozen forever: the four conditions for deadlock, and the fixes (consistent lock ordering, avoiding hold-and-wait, and timeouts).