Multithreaded C# Issues

Issues with Multithreaded Programming in General

Race Conditions

"A race condition is a bug that occurs when the outcome of a program depends on which of two or more threads reaches a particular block of code first. Running the program many times produces different results, and the result of any given run cannot be predicted." — Managed Threading Best Practices

Race condition bugs are solved with proper thread synchronization.

"When multiple threads can make calls to the properties and methods of a single object, it is critical that those calls be synchronized. Otherwise one thread might interrupt what another thread is doing, and the object could be left in an invalid state. A class whose members are protected from such interruptions is called thread-safe." — Synchronizing Data for Multithreading


"A deadlock occurs when each of two threads tries to lock a resource the other has already locked. Neither thread can make any further progress." — Managed Threading Best Practices

Deadlocks are solved by providing timeouts when trying to access a locked resources, such as with Monitor.TryEnter().

Return to CSharp or go Home