C# Multithreaded and Parallel Programming

Develop powerful C# applications to take advantage of today's multicore hardware

C# Multithreaded and Parallel Programming

Starting
Rodney Ringler

Develop powerful C# applications to take advantage of today's multicore hardware
$29.99
$49.99
RRP $29.99
RRP $49.99
eBook
Print + eBook
$12.99 p/month

Get Access

Get Unlimited Access to every Packt eBook and Video course

Enjoy full and instant access to over 3000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

+ Collection
Free Sample

Book Details

ISBN 139781849688321
Paperback344 pages

About This Book

  • Make use of the latest Visual Studio debugging tools, to manage and debug multiple threads running simultaneously
  • Learn how to use the Thread, Task, and Parallel libraries in your C# applications
  • Explore the evolution of multithreaded development in C#, starting with BackgroundWorker classes and moving on to threads and tasks and finally covering Async

Who This Book Is For

If you are a C# developer and want to learn how to take advantage of the features of .NET for concurrent and multithreaded applications, then this book is for you. If you are already comfortable with C# but want to learn more about parallel design patterns, threads, tasks, and async, then look no further!

Table of Contents

Chapter 1: Understanding Multiprocessing and Multiple Cores
Mono-processor systems – the old gladiators
Multiprocessor systems – many warriors to win a battle
Multiple core processors and hyperthreading
Examining our hardware
OS scheduler operations
Designing for concurrency
Summary
Chapter 2: Looking at Multithreaded Classes – BackgroundWorker
Getting started with the BackgroundWorker component
Simple example without a BackgroundWorker object
WPF example with an asynchronous BackgroundWorker
WPF example with a synchronous BackgroundWorker
Showing progress
Canceling a BackgroundWorker thread
Working with multiple BackgroundWorker components
Exploring other examples
Summary
Chapter 3: Thread Class – Heavyweight Concurrency in C#
Creating threads with the Thread class
Creating an application with threads
Sharing data between threads
Passing parameters to threads
Summary
Chapter 4: Advanced Thread Processing
Pipelining
Pausing and restarting threads
Signals between threads
Joining threads
Locking resources to ensure thread-safe data
Error handling with threads
Summary
Chapter 5: Lightweight Concurrency – Task Parallel Library (TPL)
Task Parallel Library
Exploring tasks
Tasks with return values
Concurrent collections
Exploring the TaskFactory class
Task schedulers
Introducing the Parallel class
Delegates and lambda expressions
Summary
Chapter 6: Task-based Parallelism
Waiting for a task to complete
Waiting for multiple tasks to complete
Canceling a task
Task exception handling
Summary
Chapter 7: Data Parallelism
Parallel loop processing
Data parallelism on collections using Parallel.ForEach
Canceling a parallel loop
Handling exceptions in parallel loops
Using thread-local variables in parallel loops
Summary
Chapter 8: Debugging Multithreaded Applications with Visual Studio
Considerations for debugging multithreaded applications
Using the Threads window
Using the Tasks window
Using the Parallel Stacks window
Using the Parallel Watch window
Debugging an entire application
Summary
Chapter 9: Pipeline and Producer-consumer Design Patterns
Pipeline design pattern
Explaining message blocks
Producer-consumer design pattern
Summary
Chapter 10: Parallel LINQ – PLINQ
Executing a PLINQ
Ordering in PLINQ
Merging in PLINQ
Canceling a PLINQ
Understanding performance improvements in PLINQ
Summary
Chapter 11: The Asynchronous Programming Model
Introduction to the Asynchronous Programming Model
Using an AsyncCallback delegate method
The async and await keywords
Summary

What You Will Learn

  • Explore all the essential methods used for programming multithreaded applications
  • Enhance the performance of an application by designing various parallel operations to achieve concurrency
  • Build powerful applications using the Task Parallel Library (TPL), which makes concurrent processing of items in a data collection simple
  • Implement data parallelism using the Parallel library, concurrent collections, and PLINQ
  • Debug your multithreaded applications using the Threads view, Tasks window, Parallel Stacks window, and Parallel Watch window
  • Accomplish any given parallel task using two of the most popular parallel patterns for development: Pipelining and producer-consumer
  • Get to grips with the Asynchronous Programming Model (APM) to learn to begin and end asynchronous operations

In Detail

Most modern machines have dual-core processors. This means that the present-day computer has the ability to multitask. Using multiple cores means your applications can process data faster and be more responsive to users. However, to fully exploit this in your applications, you need to write multithreading code.

We will begin by covering some techniques that have been around since the beginning of .NET, including the BackgroundWorker component, timers, and the Thread class. We will use tasks, task factories, and parallel loops to develop multithreaded applications at a higher level than directly creating and managing individual threads. Finally, we will look at the tools Visual Studio provides for debugging parallel applications, common concurrent design patterns, and the latest updates in PLINQ and async.

Authors

Table of Contents

Chapter 1: Understanding Multiprocessing and Multiple Cores
Mono-processor systems – the old gladiators
Multiprocessor systems – many warriors to win a battle
Multiple core processors and hyperthreading
Examining our hardware
OS scheduler operations
Designing for concurrency
Summary
Chapter 2: Looking at Multithreaded Classes – BackgroundWorker
Getting started with the BackgroundWorker component
Simple example without a BackgroundWorker object
WPF example with an asynchronous BackgroundWorker
WPF example with a synchronous BackgroundWorker
Showing progress
Canceling a BackgroundWorker thread
Working with multiple BackgroundWorker components
Exploring other examples
Summary
Chapter 3: Thread Class – Heavyweight Concurrency in C#
Creating threads with the Thread class
Creating an application with threads
Sharing data between threads
Passing parameters to threads
Summary
Chapter 4: Advanced Thread Processing
Pipelining
Pausing and restarting threads
Signals between threads
Joining threads
Locking resources to ensure thread-safe data
Error handling with threads
Summary
Chapter 5: Lightweight Concurrency – Task Parallel Library (TPL)
Task Parallel Library
Exploring tasks
Tasks with return values
Concurrent collections
Exploring the TaskFactory class
Task schedulers
Introducing the Parallel class
Delegates and lambda expressions
Summary
Chapter 6: Task-based Parallelism
Waiting for a task to complete
Waiting for multiple tasks to complete
Canceling a task
Task exception handling
Summary
Chapter 7: Data Parallelism
Parallel loop processing
Data parallelism on collections using Parallel.ForEach
Canceling a parallel loop
Handling exceptions in parallel loops
Using thread-local variables in parallel loops
Summary
Chapter 8: Debugging Multithreaded Applications with Visual Studio
Considerations for debugging multithreaded applications
Using the Threads window
Using the Tasks window
Using the Parallel Stacks window
Using the Parallel Watch window
Debugging an entire application
Summary
Chapter 9: Pipeline and Producer-consumer Design Patterns
Pipeline design pattern
Explaining message blocks
Producer-consumer design pattern
Summary
Chapter 10: Parallel LINQ – PLINQ
Executing a PLINQ
Ordering in PLINQ
Merging in PLINQ
Canceling a PLINQ
Understanding performance improvements in PLINQ
Summary
Chapter 11: The Asynchronous Programming Model
Introduction to the Asynchronous Programming Model
Using an AsyncCallback delegate method
The async and await keywords
Summary

Book Details

ISBN 139781849688321
Paperback344 pages
Read More