The 15 ways of doing concurrency in .NET
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.
- Task-based Asynchronous Pattern (TAP).
- This includes the
await
syntactic sugar. - See MSDN.
- This includes the
- Event-based Asynchronous Pattern (EAP).
- This is the
MethodNameAsync
andMethodNameCompleted
pattern. - See MSDN.
- This is the
- Asynchronous Programming Model (APM).
- This is the
BeginOperationName
andEndOperationName
pattern. - See MSDN.
- This is the
- Background Worker.
- Run an operation on a dedicated, separate thread.
- See MSDN.
- Interlocked.
- Raw Threading.
- Directly create and control a thread.
- See MSDN and this MSDN tutorial.
- Thank you @boguscoder
- Parallel Linq.
- Run LINQ to Objects’ queries in parallel.
- See MSDN.
- Thank you @mmjuraszek
- Thread Pooling.
- Add tasks to a queue and start tasks as threads become available.
- See MSDN.
- Thank you @boguscoder
- Process.
- Use
System.Diagnostics.Process.Start()
to start a process. - See MSDN.
- Thank you @mmjuraszek
- Use
- Parallel Loops.
- Control.MailBoxProcessor<`Msg>. (F#).
- Post messages to a message-processing agent, which processes the messages asynchronously.
- See MSDN.
- Thank you @monkeyonahill
- Reactive Extensions.
- Use Observables, LINQ, and Schedulers to compose asynchronous and event-based programs.
- See MSDN.
- Thank you @monkeyonahill
- Action Block
- TODO. Add a note.
- See MSDN
- Thank you @mmjuraszek
- Monitor.Wait & Monitor.Pulse with Plain Old Locks
- 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.
How many ways to do concurrency in .NET? https://t.co/4qDWcBvQHr also Interlocked.CompareExchange for lock-free concurrency @dicshaunary
— Lucian Wischik (@lwischik) November 15, 2015
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)