You are previewing Multithreading with C# Cookbook - Second Edition.
O'Reilly logo
Multithreading with C# Cookbook - Second Edition

Book Description

Over 70 recipes to get you writing powerful and efficient multithreaded, asynchronous, and parallel programs in C# 6.0

About This Book

  • Rewritten and updated to take advantage of the latest C# 6 features

  • Learn about multithreaded, asynchronous, and parallel programming through hands-on, code-first examples

  • Use these recipes to build fast, scalable, and reliable applications in C#

  • Who This Book Is For

    This book is aimed at those who are new to multithreaded programming, and who are looking for a quick and easy way to get started. It is assumed that you have some experience in C# and .NET already, and you should also be familiar with basic computer science terminology and basic algorithms and data structures.

    What You Will Learn

  • Use C# 6.0 asynchronous language features

  • Work with raw threads, synchronize threads, and coordinate their work

  • Develop your own asynchronous API with Task Parallel Library

  • Work effectively with a thread pool

  • Scale up your server application with I/O threads

  • Parallelize your LINQ queries with PLINQ

  • Use common concurrent collections

  • Apply different parallel programming patterns

  • Use Reactive Extensions to run asynchronous operations and manage their options

  • In Detail

    Multi-core processors are synonymous with computing speed and power in today’s world, which is why multithreading has become a key concern for C# developers. Multithreaded code helps you create effective, scalable, and responsive applications.

    This is an easy-to-follow guide that will show you difficult programming problems in context. You will learn how to solve them with practical, hands-on, recipes. With these recipes, you’ll be able to start creating your own scalable and reliable multithreaded applications. Starting from learning what a thread is, we guide you through the basics and then move on to more advanced concepts such as task parallel libraries, C# asynchronous functions, and much more.

    Rewritten to the latest C# specification, C# 6, and updated with new and modern recipes to help you make the most of the hardware you have available, this book will help you push the boundaries of what you thought possible in C#.

    Style and approach

    This is an easy-to-follow guide full of hands-on examples of real-world multithreading tasks. Each topic is explained and placed in context, and for the more inquisitive, there are also more in-depth details of the concepts used.

    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 code file.

    Table of Contents

    1. Multithreading with C# Cookbook Second Edition
      1. Table of Contents
      2. Multithreading with C# Cookbook Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why Subscribe?
      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. Threading Basics
        1. Introduction
        2. Creating a thread in C#
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Pausing a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Making a thread wait
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Aborting a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Determining a thread state
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Thread priority
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Foreground and background threads
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Passing parameters to a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        10. Locking with a C# lock keyword
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Locking with a Monitor construct
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Handling exceptions
          1. Getting ready
          2. How to do it...
          3. How it works...
      9. 2. Thread Synchronization
        1. Introduction
        2. Performing basic atomic operations
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Using the Mutex construct
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Using the SemaphoreSlim construct
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        5. Using the AutoResetEvent construct
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Using the ManualResetEventSlim construct
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        7. Using the CountDownEvent construct
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Using the Barrier construct
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Using the ReaderWriterLockSlim construct
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Using the SpinWait construct
          1. Getting ready
          2. How to do it...
          3. How it works...
      10. 3. Using a Thread Pool
        1. Introduction
        2. Invoking a delegate on a thread pool
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Posting an asynchronous operation on a thread pool
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. A thread pool and the degree of parallelism
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Implementing a cancellation option
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Using a wait handle and timeout with a thread pool
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        7. Using a timer
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Using the BackgroundWorker component
          1. Getting ready
          2. How to do it...
          3. How it works...
      11. 4. Using the Task Parallel Library
        1. Introduction
        2. Creating a task
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Performing basic operations with a task
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Combining tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Converting the APM pattern to tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Converting the EAP pattern to tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Implementing a cancelation option
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Handling exceptions in tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        9. Running tasks in parallel
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Tweaking the execution of tasks with TaskScheduler
          1. Getting ready
          2. How to do it...
          3. How it works...
      12. 5. Using C# 6.0
        1. Introduction
        2. Using the await operator to get asynchronous task results
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Using the await operator in a lambda expression
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Using the await operator with consequent asynchronous tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Using the await operator for the execution of parallel asynchronous tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Handling exceptions in asynchronous operations
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Avoiding the use of the captured synchronization context
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Working around the async void method
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Designing a custom awaitable type
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Using the dynamic type with await
          1. Getting ready
          2. How to do it...
          3. How it works...
      13. 6. Using Concurrent Collections
        1. Introduction
        2. Using ConcurrentDictionary
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Implementing asynchronous processing using ConcurrentQueue
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Changing asynchronous processing order with ConcurrentStack
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Creating a scalable crawler with ConcurrentBag
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Generalizing asynchronous processing with BlockingCollection
          1. Getting ready
          2. How to do it...
          3. How it works...
      14. 7. Using PLINQ
        1. Introduction
        2. Using the Parallel class
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Parallelizing a LINQ query
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Tweaking the parameters of a PLINQ query
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Handling exceptions in a PLINQ query
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Managing data partitioning in a PLINQ query
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Creating a custom aggregator for a PLINQ query
          1. Getting ready
          2. How to do it...
          3. How it works...
      15. 8. Reactive Extensions
        1. Introduction
        2. Converting a collection to an asynchronous Observable
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Writing custom Observable
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Using the Subject type family
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Creating an Observable object
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Using LINQ queries against an observable collection
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Creating asynchronous operations with Rx
          1. Getting ready
          2. How to do it...
          3. How it works...
      16. 9. Using Asynchronous I/O
        1. Introduction
        2. Working with files asynchronously
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Writing an asynchronous HTTP server and client
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Working with a database asynchronously
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Calling a WCF service asynchronously
          1. Getting ready
          2. How to do it...
          3. How it works...
      17. 10. Parallel Programming Patterns
        1. Introduction
        2. Implementing Lazy-evaluated shared states
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Implementing Parallel Pipeline with BlockingCollection
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Implementing Parallel Pipeline with TPL DataFlow
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Implementing Map/Reduce with PLINQ
          1. Getting ready
          2. How to do it...
          3. How it works...
      18. 11. There's More
        1. Introduction
        2. Using a timer in a Universal Windows Platform application
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Using WinRT from usual applications
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Using BackgroundTask in Universal Windows Platform applications
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Running a .NET Core application on OS X
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Running a .NET Core application on Ubuntu Linux
          1. Getting ready
          2. How to do it...
          3. How it works...
      19. Index