Managing deadlocks and race conditions
As with many tools at a developer’s disposal, misusing features of managed threading can have adverse impacts on your applications at runtime. Deadlocks and race conditions are two scenarios that can be created because of multithreaded programming:
- A deadlock happens when multiple threads are trying to lock the same resource and as a result, cannot continue executing.
- Race conditions happen when multiple threads are proceeding toward updating a particular routine, and a correct outcome is dependent on the order in which they execute it.
Figure 3.2 – Two threads in contention for the same resources, causing a deadlock
First, let’s discuss deadlocks and some techniques for avoiding them.
Mitigating deadlocks
It is critical to avoid deadlocks in your applications. If one of the threads involved in a deadlock is the application’s UI thread, it will cause the application to...