Book Image

Multithreading with C# Cookbook, Second Edition - Second Edition

By : Evgenii Agafonov
Book Image

Multithreading with C# Cookbook, Second Edition - Second Edition

By: Evgenii Agafonov

Overview of this book

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#.
Table of Contents (18 chapters)
Multithreading with C# Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Index

A

  • abstraction layer
    • about / Introduction
  • APM pattern
    • converting, to task / Converting the APM pattern to tasks, How to do it..., How it works...
  • AsOrdered method / How it works...
  • asynchronous functions
    • about / Introduction
    • creating / Introduction
  • asynchronous HTTP server and client
    • writing / Writing an asynchronous HTTP server and client, How it works...
  • asynchronous I/O operations
    • applications / Introduction
  • asynchronous Observable
    • collection, converting to / Converting a collection to an asynchronous Observable, How to do it..., How it works...
  • asynchronous operation
    • posting, on thread pool / Posting an asynchronous operation on a thread pool, How to do it..., How it works...
  • asynchronous operations
    • exceptions, handling / Handling exceptions in asynchronous operations, How it works...
    • creating, Rx used / Creating asynchronous operations with Rx, How to do it..., How it works...
  • asynchronous processing
    • implementing, ConcurrentQueue used / Implementing asynchronous processing using ConcurrentQueue, How to do it..., How it works...
    • generalizing, BlockingCollection used / Generalizing asynchronous processing with BlockingCollection, How it works...
  • asynchronous processing order
    • changing, ConcurrentStack used / Changing asynchronous processing order with ConcurrentStack, How to do it..., How it works...
  • Asynchronous Programming Model (APM) / Invoking a delegate on a thread pool
  • asynchronous task results
    • obtaining, with await operator / Using the await operator to get asynchronous task results, How it works...
  • async operators
    • disadvantages / Introduction
  • async void method
    • working with / Working around the async void method, How to do it..., How it works...
  • atomic operations
    • about / Introduction
    • performing / Performing basic atomic operations, How to do it..., How it works...
  • AutoResetEvent construct
    • using / Using the AutoResetEvent construct, How to do it..., How it works...
  • await operator
    • disadvantages / Introduction
    • used, for obtaining asynchronous task results / Using the await operator to get asynchronous task results, How it works...
    • using, in lambda expression / Using the await operator in a lambda expression, How it works...
    • using, with consequent asynchronous tasks / Using the await operator with consequent asynchronous tasks, How it works...
    • used, for parallel asynchronous tasks execution / Using the await operator for the execution of parallel asynchronous tasks, How it works...
    • dynamic type, using with / Using the dynamic type with await, How to do it..., How it works...

B

  • BackgroundTask
    • using, in Universal Windows Platform applications / Using BackgroundTask in Universal Windows Platform applications, How to do it..., How it works...
  • BackgroundWorker component
    • using / Using the BackgroundWorker component, How to do it..., How it works...
  • Barrier construct
    • using / Using the Barrier construct, How to do it..., How it works...
  • basic operations
    • performing, with task / Performing basic operations with a task, How to do it..., How it works...
  • BlockingCollection
    • about / Introduction
    • used, for generalizing asynchronous processing / Generalizing asynchronous processing with BlockingCollection, How it works...
    • used, for implementing Parallel Pipeline / Implementing Parallel Pipeline with BlockingCollection, How to do it..., How it works...

C

  • C#
    • thread, creating / Creating a thread in C#, How to do it..., How it works...
  • C# lock keyword
    • used, for locking / Locking with a C# lock keyword, How to do it..., How it works...
  • callback
    • registering / How it works...
  • cancelation option
    • implementing / Implementing a cancelation option, How to do it..., How it works...
  • cancellation option
    • implementing / Implementing a cancellation option, How to do it..., How it works...
  • captured synchronization context
    • use, avoiding / Avoiding the use of the captured synchronization context, How to do it..., How it works...
  • closure mechanics / How it works...
  • coarse-grained locking / How it works...
  • collection
    • converting, to asynchronous Observable / Converting a collection to an asynchronous Observable, How to do it..., How it works...
  • Common Language Runtime (CLR) / Introduction
  • Compare and Swap (CAS) / Introduction
  • ConcurrentBag
    • used, for creating scalable crawler / Creating a scalable crawler with ConcurrentBag, How it works...
  • ConcurrentDictionary
    • about / Introduction
    • using / Using ConcurrentDictionary, How it works...
  • ConcurrentQueue
    • used, for implementing asynchronous processing / Implementing asynchronous processing using ConcurrentQueue, How to do it...
  • ConcurrentStack
    • used, for changing asynchronous processing order / Changing asynchronous processing order with ConcurrentStack, How to do it..., How it works...
  • consequent asynchronous tasks
    • await operator, using with / Using the await operator with consequent asynchronous tasks, How it works...
  • context switch / Introduction
  • continuation / How it works...
  • CountDownEvent construct
    • using / Using the CountDownEvent construct, How it works...
  • custom aggregator
    • creating, for PLINQ query / Creating a custom aggregator for a PLINQ query, How to do it..., How it works...
  • custom awaitable type
    • designing / Designing a custom awaitable type, How it works...
  • custom Observable
    • writing / Writing custom Observable, How to do it..., How it works...

D

  • database
    • working with, asynchronously / Working with a database asynchronously, How it works...
  • data parallelism / Introduction
  • data partitioning
    • managing in PLINQ query / Managing data partitioning in a PLINQ query, How to do it..., How it works...
  • degree of parallelism
    • and thread pool / A thread pool and the degree of parallelism, How to do it..., How it works...
  • delegate
    • about / Introduction
    • invoking, on thread pool / Invoking a delegate on a thread pool, How to do it..., How it works...
  • double-checked locking pattern / How it works...
  • dynamic type
    • using, with await operator / Using the dynamic type with await, How to do it..., How it works...

E

  • EAP pattern
    • converting, to task / Converting the EAP pattern to tasks, How it works...
  • Enqueue method / Introduction
  • Event-based Asynchronous Pattern (EAP) / How it works...
  • event handlers / How it works...
  • events / How it works...
  • exception handling, in task
    • about / Getting ready, How it works...
  • exceptions
    • handling / Handling exceptions, How it works...
  • exceptions, asynchronous operations
    • handling / Handling exceptions in asynchronous operations, How it works...
  • exceptions, PLINQ query
    • handling / Getting ready, How to do it..., How it works...

F

  • files
    • working with, asynchronously / Working with files asynchronously, How it works...
  • fine-grained locking technique / How it works...
  • First In, First Out (FIFO) / Introduction

G

  • Gutenberg
    • website link / How it works...

H

  • hybrid constructs / Introduction

I

  • I/O threads / Introduction
  • iterators / Introduction

K

  • kernel-mode constructs / Introduction

L

  • lambda expression / How it works...
    • await operator, using / Using the await operator in a lambda expression, How it works...
  • Last In, First Out (LIFO) / Introduction
  • Lazy-evaluated shared states
    • implementing / Implementing Lazy-evaluated shared states, How it works...
  • LazyInitializer.EnsureInitialized method / How it works...
  • LINQ queries
    • using, against observable collection / Using LINQ queries against an observable collection, How to do it..., How it works...
  • LINQ query
    • parallelizing / Parallelizing a LINQ query, How to do it..., How it works...

M

  • ManualResetEventSlim construct
    • using / Using the ManualResetEventSlim construct, How to do it..., How it works...
  • Map/Reduce pattern
    • about / Introduction
    • implementing, with PLINQ / Implementing Map/Reduce with PLINQ, How to do it..., How it works...
  • monitor construct
    • locking with / Locking with a Monitor construct, How to do it..., How it works...
  • Mutex construct
    • using / Using the Mutex construct, How it works...

N

  • .NET Core
    • on OS X, installation link / How to do it...
    • on Ubuntu machine, installation link / How to do it...
  • .NET Core application
    • running, on OS X / Running a .NET Core application on OS X, How to do it..., How it works...
    • running, on Ubuntu Linux / Running a .NET Core application on Ubuntu Linux, How to do it..., How it works...
  • .NET thread pool / Introduction

O

  • observable collection
    • LINQ queries, using against / Using LINQ queries against an observable collection, How it works...
  • Observable object
    • creating / Creating an Observable object, How to do it..., How it works...
  • OS X
    • .NET Core application, running / Running a .NET Core application on OS X, How to do it..., How it works...

P

  • parallel asynchronous tasks
    • executing, await operator used / Getting ready, How it works...
  • Parallel class
    • using / Using the Parallel class, How it works...
  • Parallel Framework Extensions (PFX) / Introduction
  • Parallel Pipeline
    • about / Introduction
    • implementing, with BlockingCollection / Implementing Parallel Pipeline with BlockingCollection, How to do it..., How it works...
    • implementing, with TPL DataFlow / Implementing Parallel Pipeline with TPL DataFlow, How to do it..., How it works...
  • parameters
    • passing, to thread / Passing parameters to a thread, How to do it..., There's more…
  • parameters, PLINQ query
    • tweaking / Tweaking the parameters of a PLINQ query, How to do it..., How it works...
  • PLINQ
    • used, for implementing Map/Reduce / Getting ready, How to do it..., How it works...
  • PLINQ query
    • parameters, tweaking / Tweaking the parameters of a PLINQ query, How to do it..., How it works...
    • exceptions, handling / Handling exceptions in a PLINQ query, How to do it..., How it works...
    • data partitioning, managing / Managing data partitioning in a PLINQ query, How to do it..., How it works...
    • custom aggregator, creating / Creating a custom aggregator for a PLINQ query, How to do it..., How it works...
  • pooling / Introduction
  • pull-based approach / Introduction
  • push-based approach / Introduction

R

  • Reactive Extensions (Rx)
    • about / Introduction
    • used, for creating asynchronous operations / Creating asynchronous operations with Rx, How to do it..., How it works...
  • ReaderWriterLockSlim construct
    • using / Using the ReaderWriterLockSlim construct, How it works...

S

  • scalable crawler
    • creating, ConcurrentBag used / Creating a scalable crawler with ConcurrentBag, How it works...
  • SemaphoreSlim construct
    • using / Using the SemaphoreSlim construct, How to do it..., How it works...
  • shared-state object / Introduction
  • Simple Object Access Protocol (SOAP) / How it works...
  • SpinWait construct
    • using / Using the SpinWait construct, How it works...
  • structured parallelism / Introduction
  • Subjects type
    • using / Using the Subject type family, How to do it..., How it works...

T

  • task
    • about / Introduction
    • creating / Creating a task, How it works...
    • basic operations, performing with / Performing basic operations with a task, How to do it..., How it works...
    • combining / Combining tasks, How to do it..., How it works...
    • APM pattern, converting to / Converting the APM pattern to tasks, How to do it..., How it works...
    • EAP pattern, converting to / Converting the EAP pattern to tasks, How it works...
    • exception handling / Handling exceptions in tasks, How it works...
    • running, in parallel / Running tasks in parallel, How to do it..., How it works...
  • task execution
    • tweaking, with TaskScheduler / Tweaking the execution of tasks with TaskScheduler, How to do it..., How it works...
  • task parallelism / Introduction
  • Task Parallel Library / Introduction
  • Task Parallel Library (TPL) / How it works...
  • task scheduler / How it works...
  • thread
    • creating, in C# / Creating a thread in C#, How to do it..., How it works...
    • pausing / Pausing a thread, How it works...
    • making, wait / Making a thread wait, How it works...
    • aborting / Aborting a thread, How it works...
    • state, determining / Determining a thread state, How it works...
    • priority / Thread priority, How to do it..., How it works...
    • foreground / Foreground and background threads, How to do it...
    • background / Foreground and background threads, How it works...
    • parameters, passing / Passing parameters to a thread, How to do it..., There's more…
    • cancellation option, implementing / How to do it..., How it works...
  • thread pool
    • delegate, invoking / Invoking a delegate on a thread pool, How it works...
    • asynchronous operation, posting / Posting an asynchronous operation on a thread pool, How to do it..., How it works...
    • and degree of parallelism / A thread pool and the degree of parallelism, How to do it..., How it works...
    • wait handle, using / Using a wait handle and timeout with a thread pool, How it works...
    • timeout, using / Getting ready, How it works..., There's more…
  • thread synchronization
    • about / Introduction
    • Mutex construct / Using the Mutex construct, How it works...
    • SemaphoreSlim construct / Using the SemaphoreSlim construct, How it works...
    • AutoResetEvent construct / Using the AutoResetEvent construct, How to do it..., How it works...
    • ManualResetEventSlim construct / Using the ManualResetEventSlim construct, How to do it..., There's more…
    • CountDownEvent construct / Using the CountDownEvent construct, How to do it..., How it works...
    • Barrier construct / Using the Barrier construct, How it works...
    • ReaderWriterLockSlim construct / Using the ReaderWriterLockSlim construct, How to do it..., How it works...
    • SpinWait construct / Using the SpinWait construct, How to do it..., How it works...
  • timeout
    • using, with thread pool / Using a wait handle and timeout with a thread pool, How to do it..., How it works...
  • timer
    • using / Using a timer, How it works...
    • using, in Universal Windows Platform application / Using a timer in a Universal Windows Platform application, How to do it..., How it works...
  • TPL DataFlow
    • about / Introduction
    • used, for implementing, Parallel Pipeline / Implementing Parallel Pipeline with TPL DataFlow, How to do it..., How it works...
  • TryDequeue method / Introduction
  • TryPeek method / Introduction

U

  • Ubuntu Linux
    • .NET Core application, running / Running a .NET Core application on Ubuntu Linux, How to do it..., How it works...
  • Universal Windows Platform application
    • timer, using / Using a timer in a Universal Windows Platform application, How to do it..., How it works...
    • BackgroundTask, using / Using BackgroundTask in Universal Windows Platform applications, How to do it..., How it works...
  • unstructured parallelism / Introduction
  • user-mode constructs / Introduction

W

  • wait handle
    • using, with thread pool / Using a wait handle and timeout with a thread pool, How it works..., There's more…
  • Windows Communication (WCF) service
    • about / Introduction
    • calling, asynchronously / Calling a WCF service asynchronously, How to do it..., How it works...
  • Windows Forms Applications (WPF) / How it works...
  • WinRT
    • about / Introduction
    • using, from usual applications / Using WinRT from usual applications, How to do it..., How it works...
  • WithExecutionMode method / How it works...
  • WithMergeOptions method / How it works...
  • worker thread / Introduction