Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

The 15 ways of doing concurrency in .NET

Tags: c#, Concurrency, .net, parallel-programming

A reliable source told me that there are 15 ways to do concurrency in .NET. The goal of this post is to write them all down with a brief note for each.

I am not an expert in concurrency, so will not succeed without your help. Do you know one that I missed? Tell me on Twitter @dicshaunary.

  1. Task-based Asynchronous Pattern (TAP).
    • This includes the await syntactic sugar.
    • See MSDN.
  2. Event-based Asynchronous Pattern (EAP).
    • This is the MethodNameAsync and MethodNameCompleted pattern.
    • See MSDN.
  3. Asynchronous Programming Model (APM).
    • This is the BeginOperationName and EndOperationName pattern.
    • See MSDN.
  4. Background Worker.
    • Run an operation on a dedicated, separate thread.
    • See MSDN.
  5. Interlocked. 
    • Run atomic operations on variables that exist on multiple threads.
    • “Lock free concurrency.”
    • See MSDN.
    • Thank you @lwischik
  6. Raw Threading.
  7. Parallel Linq.
  8. Thread Pooling.
    • Add tasks to a queue and start tasks as threads become available.
    • See MSDN. 
    • Thank you @boguscoder
  9. Process.
  10. Parallel Loops.
    • Use Parallel.For and Parallel.ForEach  to run loops in parallel.
    • See MSDN.
    • Thank you @lwischik
  11. Control.MailBoxProcessor<`Msg>. (F#).
    • Post messages to a message-processing agent, which processes the messages asynchronously.
    • See MSDN.
    • Thank you @monkeyonahill
  12. Reactive Extensions.
    • Use Observables, LINQ, and Schedulers to compose asynchronous and event-based programs.
    • See MSDN.
    • Thank you @monkeyonahill
  13. Action Block
  14. Monitor.Wait & Monitor.Pulse with Plain Old Locks
    • This is syntactic sugar over top of Plain Old Locks.
    • See MSDN
    • Thank you @gusev_p
  15. Plain Old Locks

Know one that isn’t listed? Help the list reach 15 on Twitter @dicshaunary.

Thank you to Lucian for starting the conversation.

TODO

  • Organize the above into paradigms and their various implementations.
  • Add an example for each, perhaps as a Gist or a .NET Fiddle.
  • Add advantages and disadvantages of each.
  • Add appropriate use cases for each.
  • Legacy vs current. Make a note of which are current and which are legacy. (Thank you Reddit https://redd.it/3uvs84)