Parallel and Asynchronous Programming in C#

In recent years, parallel hardware has become the norm. Its not unusual to discover that ones smartphone has a multi-core CPU these days! At the same time, making calls to remote services has become common and users have come to expect highly responsive interfaces - both of which push developers towards asynchronous programming.

Thankfully, the .Net Framework and C# language have been evolving to help us cope with these new realities and expectations. Of course, there have always been threads and locks - but today, weve many, many more options: concurrent collections, parallel looping constructs, PLINQ, Tasks, the new async/await keywords in C# 5, the Reactive Extensions (Rx), the TPL Dataflow library with its Actor-based approach... Each of them are suitable for different problems. So how do you know what to use when, and how to use them effectively? 

Thats where this course comes in. It wont just teach you how to use all of these exciting new libraries. It will also help you to understand what kind of problem each one is aimed at, so that you can pick the appropriate solution to the challenges you face. It also provides clear explanations of pertinent theory and terminology, including a dive all the way down to the hardware level, so youll have a strong grasp on whats really going on! Labs will provide an opportunity for you to try things out for yourself. 


Experienced C# / .Net developers who need to learn about asynchronous and parallel programming. 


Good knowledge of C#, especially generics, lambdas and LINQ. Previous experience of parallel or async programming is not required. 

About the instructor: Jonathan Worthington

From business applications to compiler writing, and from .Net to Perl, Jonathan has a wide range of software development experience. He deeply believes that good development has to be a strongly holistic activity, drawing on mathematics, engineering, linguistics, economics, psychology and more. By looking at insights from many fields, he works hard to deliver solid and maintainable software solutions. Originally from the UK, and having spent time in Spain and Slovakia, Jonathan is currently based in Sweden. 
Jonathan is also a very active open source contributor, and a popular speaker at a range of international workshops and conferences.

Course outline:

  • Classifying Concurrency

    • Parallel vs. asynchronous
    • Task parallel vs. data parallel
    • CPU-bound vs. IO-bound
    • Shared vs. thread-local data
    • Blocking, lock-free, wait-free, and progress bounds


    Understanding The Hardware

    • Why did CPUs go multi-core?
    • How CPU caches work with multi-core, and why it matters
    • Why therad-local storage helps
    • Why immutability helps
    • The problem of false sharing


    Building Blocks

    • Threads
    • How many threads?
    • The thread pool
    • The Task abstraction



    • Monitors, the lock keyword
    • Multiple Reader, Single Writer locks
    • Deadlock
    • Lock granularity
    • The cost of locking
    • Avoiding locking


    Concurrent Collections

    • What are the concurrent collections?
    • Concurrent queues, stacks, bags and dictionaries
    • The blocking collection
    • Queues and Staged Event Driven Architecture
    • Other use cases
    • Performance compared to locking the non-concurrent collections


    The Parallel Class

    • Parallel for
    • Parallel foreach
    • Flow control in parallel loops
    • Keeping thread local state in parallel loops
    • Exceptions and parallel loops
    • Cancellation
    • Parallel invoke
  • Asynchronous Programming

    • Why threads arent ideal for blocking operations
    • ContinueWith and CPS (Continuation Passing Style)
    • Why CPS transforms hurt, and how async/await help
    • Exploring the new Async methods in .Net 4.5
    • Task combinators


    More On Asynchrony

    • Understanding synchronization contexts
    • Cancellation
    • Task completion sources
    • Integrating asynchrony and parallelism


    Introducing Rx

    • The Enumerable/Observable duality
    • IObserver and IObservable
    • Producing and consuming simple event streams
    • Subscription and unsubscription
    • Familiar Linq Operators, the Rx way
    • Converting enumerables to observables, and vice versa


    Applying Rx

    • Using Rx with Windows Forms
    • Using Rx with WPF
    • Using Rx with async/await
    • Understanding schedulers
    • Writing tests for Rx code
    • Other useful Rx bits
  • TPL DataFlow

    • Agents and actors: the ideas behind dataflow parallelism
    • Sources, targets and propagators
    • Transforming
    • Joining
    • Batching
    • Schedulers
    • Implementing custom blocks


    Parallel LINQ

    • A quick LINQ refresher
    • Using PLINQ
    • Understanding how PLINQ parallelizes queries
    • Order (non-)preservation
    • Merge options
    • Cancellation
    • Dealing with exceptions
    • Custom aggregation functions


    Atomic Operations and Lock Free Data Structures

    • The Interlocked class
    • Basic atomic operations
    • Atomic Compare And Swap (CAS)
    • How to make any data structure lock free - at a cost
    • Livelock, and the contrast with wait freedom
    • The A-B problem


    Putting It All Together

    • Mapping problems to suitable concurrent mechanisms
    • Integrating approaches
    • The importance of immutability
    • Trends so far, and how the future may look

Other relevant courses