↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

.NET Threading Gotchas

The article reviews five common threading mistakes in .NET applications and explains how to avoid them. It focuses on cases where developers may accidentally make threading harder or slower while trying to handle asynchronous or multi-threaded code.

Reading notes
#

  • Using async methods synchronously can add threading overhead without gaining parallelism, so the article recommends Task.WhenAll when the operations can run independently.
  • Await resumes on the original SynchronizationContext by default, which can create delays or deadlocks in busy environments; ConfigureAwait(false) avoids that when the thread context does not matter.
  • When multiple threads access the same collection, the article advises using locks or concurrent collections such as ConcurrentDictionary instead of assuming ordinary collections are safe.
  • Static classes and static state are presented as risky because they can be accessed by multiple threads and can also make parallel unit tests inconsistent.
  • A simple increment is not a single operation at the CLR level, so race conditions can lose updates when many threads touch the same field.
  • For shared numeric state, the article recommends Interlocked methods to perform atomic updates instead of managing the field with a lock.
  • The article points readers to the Async Guidance page on GitHub for more detailed threading guidance.