You are previewing C# Multithreaded and Parallel Programming.
O'Reilly logo
C# Multithreaded and Parallel Programming

Book Description

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

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.

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
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. C# Multithreaded and Parallel Programming
      1. Table of Contents
      2. C# Multithreaded and Parallel Programming
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
          3. Instant updates on new Packt books
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Understanding Multiprocessing and Multiple Cores
        1. Mono-processor systems – the old gladiators
          1. Single core – only one warrior to fight against everybody
          2. Doing a tiny bit of each task
        2. Multiprocessor systems – many warriors to win a battle
        3. Multiple core processors and hyperthreading
          1. Taking advantage of multiple execution cores
        4. Examining our hardware
        5. OS scheduler operations
        6. Designing for concurrency
          1. Estimating performance improvements
          2. Amdahl's law
          3. Gustafson's law
        7. Summary
      9. 2. Looking at Multithreaded Classes – BackgroundWorker
        1. Getting started with the BackgroundWorker component
        2. Simple example without a BackgroundWorker object
          1. How to do it
          2. How does it work?
        3. WPF example with an asynchronous BackgroundWorker
          1. How to do it
          2. How does it work?
          3. How does it work without blocking the UI?
          4. How to do it
          5. How does it work?
        4. WPF example with a synchronous BackgroundWorker
        5. Showing progress
          1. How to do it
          2. How does it work?
        6. Canceling a BackgroundWorker thread
          1. How to do it
          2. How does it work?
        7. Working with multiple BackgroundWorker components
          1. How does it work?
          2. How does it work?
        8. Exploring other examples
        9. Summary
      10. 3. Thread Class – Heavyweight Concurrency in C#
        1. Creating threads with the Thread class
          1. Let's get started with an encryption program
          2. How to do it
          3. How it works
        2. Creating an application with threads
          1. How to do it
          2. How it works
        3. Sharing data between threads
          1. How to do it
          2. How it works
        4. Passing parameters to threads
          1. How to do it
          2. How it works
          3. Have a go hero – concurrent UI feedback
        5. Summary
      11. 4. Advanced Thread Processing
        1. Pipelining
          1. Explaining pipelining using an image processing application
          2. How to do it
          3. How it works
          4. Understanding the pixels' color compositions
        2. Pausing and restarting threads
          1. How to do it
          2. How it works
        3. Signals between threads
          1. How to do it
          2. How it works
          3. Using the AutoResetEvent class to handle signals between threads
          4. Using the WaitHandle class to check for signals
        4. Joining threads
          1. How to do it
          2. How it works
        5. Locking resources to ensure thread-safe data
          1. How to do it
          2. How it works
        6. Error handling with threads
          1. How to do it
          2. How it works
        7. Summary
      12. 5. Lightweight Concurrency – Task Parallel Library (TPL)
        1. Task Parallel Library
        2. Exploring tasks
          1. How to do it
          2. How it works
        3. Tasks with return values
          1. How to do it
          2. How it works
        4. Concurrent collections
          1. How to do it
          2. How it works
        5. Exploring the TaskFactory class
          1. How to do it
          2. How it works
        6. Task schedulers
        7. Introducing the Parallel class
          1. How to do it
          2. How it works
        8. Delegates and lambda expressions
        9. Summary
      13. 6. Task-based Parallelism
        1. Waiting for a task to complete
          1. How to do it
          2. How it works
        2. Waiting for multiple tasks to complete
          1. How to do it
          2. How it works
        3. Canceling a task
          1. How to do it
          2. How it works
        4. Task exception handling
        5. Summary
      14. 7. Data Parallelism
        1. Parallel loop processing
          1. How to do it
          2. How it works
        2. Data parallelism on collections using Parallel.ForEach
          1. How to do it
          2. How it works
        3. Canceling a parallel loop
          1. How to do it
          2. How it works
        4. Handling exceptions in parallel loops
          1. How to do it
          2. How it works
        5. Using thread-local variables in parallel loops
          1. How to do it
          2. How it works
        6. Summary
      15. 8. Debugging Multithreaded Applications with Visual Studio
        1. Considerations for debugging multithreaded applications
        2. Using the Threads window
        3. Using the Tasks window
        4. Using the Parallel Stacks window
        5. Using the Parallel Watch window
        6. Debugging an entire application
          1. How to do it
          2. How it works
        7. Summary
      16. 9. Pipeline and Producer-consumer Design Patterns
        1. Pipeline design pattern
          1. How to do it
          2. How it works
        2. Explaining message blocks
          1. BufferBlock
          2. ActionBlock
        3. Producer-consumer design pattern
          1. How to do it
          2. How it works
        4. Summary
      17. 10. Parallel LINQ – PLINQ
        1. Executing a PLINQ
          1. How to do it
          2. How it works
        2. Ordering in PLINQ
          1. How to do it
          2. How it works
        3. Merging in PLINQ
          1. How to do it
          2. How it works
        4. Canceling a PLINQ
          1. How to do it
          2. How it works
        5. Understanding performance improvements in PLINQ
        6. Summary
      18. 11. The Asynchronous Programming Model
        1. Introduction to the Asynchronous Programming Model
          1. How to do it
          2. How it works
        2. Using an AsyncCallback delegate method
          1. How to do it
          2. How it works
        3. The async and await keywords
          1. How to do it
          2. How it works
        4. Summary
      19. Index