Deadlock occurs when 4 conditions are met: Mutual Exclusion, Hold and Wait, No Preemption, and Circular Wait. To prevent: Break one of these 4 conditions. Most common: Lock Ordering (always acquire locks in the same order), Timeouts, or avoiding nested locks.
Traffic intersections use deadlock prevention by enforcing traffic lights. Cars can't wait forever while holding a resource.
How can you prevent a deadlock in your code? Provide code examples.