ENGLISH

Hands-On Parallel Programming with C# 8 and .NET Core 3: Build solid enterprise software using task parallelism and multithreading

Book information

Publisher
Packt Publishing
Year
2019
ISBN
178913241X, 9781789132410
Language
english
Format
PDF
Filesize
5 MB (5127696 bytes)
Pages
346\328
Time added
2020-05-25 16:56:50

Description

Enhance your enterprise application development skills by mastering parallel programming techniques in .NET and C# Key Features Write efficient, fine-grained, and scalable parallel code with C# and .NET Core Experience how parallel programming works by building a powerful application Learn the fundamentals of multithreading by working with IIS and KestrelBook Description In today's world, every CPU has a multi-core processor. However, unless your application has implemented parallel programming, it will fail to utilize the hardware's full processing capacity. This book will show you how to write modern software on the optimized and high-performing .NET Core 3 framework using C# 8. Hands-On Parallel Programming with C# 8 and .NET Core 3 covers how to build multithreaded, concurrent, and optimized applications that harness the power of multi-core processors. Once you've understood the fundamentals of threading and concurrency, you'll gain insights into the data structure in .NET Core that supports parallelism. The book will then help you perform asynchronous programming in C# and diagnose and debug parallel code effectively. You'll also get to grips with the new Kestrel server and understand the difference between the IIS and Kestrel operating models. Finally, you'll learn best practices such as test-driven development, and run unit tests on your parallel code. By the end of the book, you'll have developed a deep understanding of the core concepts of concurrency and asynchrony to create responsive applications that are not CPU-intensive. What you will learn Analyze and break down a problem statement for parallelism Explore the APM and EAP patterns and how to move legacy code to Task Apply reduction techniques to get aggregated results Create PLINQ queries and study the factors that impact their performance Solve concurrency problems caused by producer-consumer race conditions Discover the synchronization primitives available in .NET Core Understand how the threading model works with IIS and Kestrel Find out how you can make the most of server resourcesWho this book is for If you want to learn how task parallelism is used to build robust and scalable enterprise architecture, this book is for you. Whether you are a beginner to parallelism in C# or an experienced architect, you'll find this book useful to gain insights into the different threading models supported in .NET Standard and .NET Core. Prior knowledge of C# is required to understand the concepts covered in this book. Table of Contents Introduction to Parallel Programming Task Parallelism Data Parallelism Using PLINQ Synchronization Primitives Using Concurrent Collections Improving Performance with Lazy Introduction to Asynchronous Programming Async, Await and Task Based Asynchronous programming basics Debugging Tasks using Visual Studio Writing Unit Test cases for Parallel and Asynchronous code IIS and Kestrel in Asp.net Core Patterns in Parallel Programming Distributed memory management Cover Title Page Copyright and Credits Dedication About Packt Contributors Table of Contents Preface Section 1: Fundamentals of Threading, Multitasking, and Asynchrony Chapter 1: Introduction to Parallel Programming Technical requirements Preparing for multi-core computing Processes Some more information about the OS Multitasking Hyper-threading Flynn's taxonomy Threads Types of threads Apartment state Multithreading Thread class Advantages and disadvantages of threads The ThreadPool class Advantages, disadvantages, and when to avoid using ThreadPool BackgroundWorker Advantages and disadvantages of using BackgroundWorker Multithreading versus multitasking Scenarios where parallel programming can come in handy Advantages and disadvantages of parallel programming  Summary Questions Chapter 2: Task Parallelism Technical requirements Tasks Creating and starting a task The System.Threading.Tasks.Task class Using lambda expressions syntax Using the Action delegate Using delegate The System.Threading.Tasks.Task.Factory.StartNew method Using lambda expressions syntax Using the Action delegate Using delegate The System.Threading.Tasks.Task.Run method Using lambda expressions syntax Using the Action delegate Using delegate The System.Threading.Tasks.Task.Delay method The System.Threading.Tasks.Task.Yield method The System.Threading.Tasks.Task.FromResult method The System.Threading.Tasks.Task.FromException and System.Threading.Tasks.Task.FromException methods The System.Threading.Tasks.Task.FromCanceled and System.Threading.Tasks.Task.FromCanceled methods Getting results from finished tasks How to cancel tasks Creating a token Creating a task using tokens Polling the status of the token via the IsCancellationRequested property  Registering for a request cancellation using the Callback delegate How to wait on running tasks Task.Wait Task.WaitAll Task.WaitAny Task.WhenAll Task.WhenAny Handling task exceptions Handling exception from single tasks Handling exceptions from multiple tasks Handling task exceptions with a callback function Converting APM patterns into tasks Converting EAPs into tasks More on tasks Continuation tasks Continuing tasks using the Task.ContinueWith method Continuing tasks using Task.Factory.ContinueWhenAll and Task.Factory.ContinueWhenAll Continuing tasks using Task.Factory.ContinueWhenAny and Task.Factory.ContinueWhenAny Parent and child tasks Creating a detached task Creating an attached task Work-stealing queues Summary Chapter 3: Implementing Data Parallelism Technical requirements Moving from sequential loops to parallel loops Using the Parallel.Invoke method Using the Parallel.For method Using the Parallel.ForEach method Understanding the degree of parallelism Creating a custom partitioning strategy Range partitioning Chunk partitioning Canceling loops Using the Parallel.Break method Using ParallelLoopState.Stop Using CancellationToken to cancel loops Understanding thread storage in parallel loops Thread local variable Partition local variable Summary Questions Chapter 4: Using PLINQ Technical requirements LINQ providers in .NET Writing PLINQ queries Introducing the ParallelEnumerable class Our first PLINQ query Preserving order in PLINQ while doing parallel executions Sequential execution using the AsUnOrdered() method Merge options in PLINQ Using the NotBuffered merge option Using the AutoBuffered merge option Using the FullyBuffered merge option Throwing and handling exceptions with PLINQ Combining parallel and sequential LINQ queries Canceling PLINQ queries Disadvantages of parallel programming with PLINQ Understanding the factors that affect the performance of PLINQ (speedups) Degree of parallelism Merge option Partitioning type Deciding when to stay sequential with PLINQ Order of operation ForAll versus calling ToArray() or ToList() Forcing parallelism Generating sequences Summary Questions Section 2: Data Structures that Support Parallelism in .NET Core Chapter 5: Synchronization Primitives Technical requirements What are synchronization primitives? Interlocked operations  Memory barriers in .NET What is reordering?  Types of memory barriers Avoiding code reordering using constructs Introduction to locking primitives How locking works Thread state Blocking versus spinning Lock, mutex, and semaphore Lock Mutex Semaphore Local semaphore Global semaphore ReaderWriterLock Introduction to signaling primitives Thread.Join EventWaitHandle AutoResetEvent ManualResetEvent WaitHandles Lightweight synchronization primitives Slim locks ReaderWriterLockSlim SemaphoreSlim ManualResetEventSlim Barrier and countdown events A case study using Barrier and CountDownEvent SpinWait SpinLock Summary Questions Chapter 6: Using Concurrent Collections Technical requirements An introduction to concurrent collections Introducing IProducerConsumerCollection Using ConcurrentQueue Using queues to solve a producer-consumer problem Solving problems using concurrent queues Performance consideration – Queue versus ConcurrentQueue Using ConcurrentStack Creating a concurrent stack Using ConcurrentBag Using BlockingCollection Creating BlockingCollection A multiple producer-consumer scenario Using ConcurrentDictionary Summary Questions Chapter 7: Improving Performance with Lazy Initialization Technical requirements Introducing lazy initialization concepts Introducing System.Lazy Construction logic encapsulated inside a constructor Construction logic passed as a delegate to Lazy Handling exceptions with the lazy initialization pattern No exceptions occur during initialization Random exception while initialization with exception caching Not caching exceptions Lazy initialization with thread-local storage Reducing the overhead with lazy initializations Summary Questions Section 3: Asynchronous Programming Using C# Chapter 8: Introduction to Asynchronous Programming Technical requirements Types of program execution Understanding synchronous program execution Understanding asynchronous program execution When to use asynchronous programming Writing asynchronous code Using the BeginInvoke method of the Delegate class Using the Task class Using the IAsyncResult interface When not to use asynchronous programming In a single database without connection pooling When it is important that the code is easy to read and maintain For simple and short-running operations For applications with lots of shared resources Problems you can solve using asynchronous code Summary Questions Chapter 9: Async, Await, and Task-Based Asynchronous Programming Basics Technical requirements Introducing async and await The return type of async methods Async delegates and lambda expressions Task-based asynchronous patterns The compiler method, using the async keyword Implementing the TAP manually Exception handling with async code A method that returns Task and throws an exception An async method from outside a try-catch block without the await keyword An async method from inside the try-catch block without the await keyword Calling an async method with the await keyword from outside the try-catch block Methods returning void Async with PLINQ Measuring the performance of async code Guidelines for using async code Avoid using async void Async chain all the way Using ConfigureAwait wherever possible Summary Questions Section 4: Debugging, Diagnostics, and Unit Testing for Async Code Chapter 10: Debugging Tasks Using Visual Studio Technical requirements Debugging with VS 2019  How to debug threads Using Parallel Stacks windows Debugging using Parallel Stacks windows Threads view Tasks view Debugging using the Parallel Watch window Using Concurrency Visualizer Utilization view Threads view Cores view Summary Questions Further reading  Chapter 11: Writing Unit Test Cases for Parallel and Asynchronous Code Technical requirements Unit testing with .NET Core Understanding the problems with writing unit test cases for async code Writing unit test cases for parallel and async code Checking for a successful result Checking for an exception result when the divisor is 0 Mocking the setup for async code using Moq Testing tools Summary Questions  Further reading Section 5: Parallel Programming Feature Additions to .NET Core Chapter 12: IIS and Kestrel in ASP.NET Core Technical requirements IIS threading model and internals Starvation Avoidance Hill Climbing Kestrel threading model and internals ASP.NET Core 1.x ASP.NET Core 2.x Introducing the best practices of threading in microservices Single thread-single process microservices Single thread-multiple process microservices Multiple threads-single process Asynchronous services Dedicated thread pools Introducing async in ASP.NET MVC core Async streams  Summary Questions Chapter 13: Patterns in Parallel Programming Technical requirements The MapReduce pattern Implementing MapReduce using LINQ Aggregation The fork/join pattern The speculative processing pattern The lazy pattern Shared state pattern Summary Questions Chapter 14: Distributed Memory Management Technical requirements Introduction to distributed systems Shared versus distributed memory model Shared memory model Distributed memory model Types of communication network Static communication networks Dynamic communication networks Properties of communication networks Topology Routing algorithms Switching strategy Flow control Exploring topologies Linear and ring topologies Linear arrays Ring or torus Meshes and tori 2D mesh 2D torus Programming distributed memory machines using message passing Why MPI? Installing MPI on Windows Sample program using MPI Basic send/receive use Collectives Summary Questions Assessments Other Books You May Enjoy Index

Similar books

Session C11: Ancient Cultural Landscapes in South Europe – their Ecological Setting and Evolution, Session C22: Gardeners from South America, Session S04: Agro-Pastoralism and Early Metallurgy Sessions, Session WS29: The Idea of Enclosure in Recent Iberian Prehistory, Session C88: Rhytmes et causalites des dynamiques de l'anthropisation en Europe entre 6500 ET 500 BC: Hypotheses socio-culturelles et/ou climatiques: Proceedings of the XV UISPP World Congress (Lisbon 4-9 September 2006) / Actes du XV Congrès Mondial (Lisbonne 4-9 Septembre 2006) Vol.36

Session C11: Ancient Cultural Landscapes in South Europe – their Ecological Setting and Evolution, Session C22: Gardeners from South America, Session S04: Agro-Pastoralism and Early Metallurgy Sessions, Session WS29: The Idea of Enclosure in Recent Iberian Prehistory, Session C88: Rhytmes et causalites des dynamiques de l'anthropisation en Europe entre 6500 ET 500 BC: Hypotheses socio-culturelles et/ou climatiques: Proceedings of the XV UISPP World Congress (Lisbon 4-9 September 2006) / Actes du XV Congrès Mondial (Lisbonne 4-9 Septembre 2006) Vol.36

2010 · PDF

THE BRITISH ARMY IN INDIA: ITS PRESERVATION BY AN APPROPRIATE CLOTHING, HOUSING, LOCATING, RECREATIVE EMPLOYMENT, AND HOPEFUL ENCOURAGEMENT OF THE TROOPS. with AN APPENDIX ON INDIA : THE CLIMATE OP ITS HILLS ; THE DEVELOPMENT OF ITS RESODRCBS, INDUSTRY, AND ARTS ; THE ADMINISTRATION OF JUSTICE ; THE BLACK ACT ; THE PROGRESS OF CHRISTIANITY ; THE TRAFFIC IN OPIUM ; THE VALUE OF INDIA ; PERMANENT CAUSES OF DISAFFECTION, AND OF THE RECENT REBELLION ; THE TRADITIONARY POLICY; MISGOVERNMENT BY NATIVE RULERS ; ANNEXATIONS OF THEIR TERRITORY, ETC.

THE BRITISH ARMY IN INDIA: ITS PRESERVATION BY AN APPROPRIATE CLOTHING, HOUSING, LOCATING, RECREATIVE EMPLOYMENT, AND HOPEFUL ENCOURAGEMENT OF THE TROOPS. with AN APPENDIX ON INDIA : THE CLIMATE OP ITS HILLS ; THE DEVELOPMENT OF ITS RESODRCBS, INDUSTRY, AND ARTS ; THE ADMINISTRATION OF JUSTICE ; THE BLACK ACT ; THE PROGRESS OF CHRISTIANITY ; THE TRAFFIC IN OPIUM ; THE VALUE OF INDIA ; PERMANENT CAUSES OF DISAFFECTION, AND OF THE RECENT REBELLION ; THE TRADITIONARY POLICY; MISGOVERNMENT BY NATIVE RULERS ; ANNEXATIONS OF THEIR TERRITORY, ETC.

1858 · PDF

Idries Shah 27 Books Collection : A Perfumed Scorpion, A Veiled Gazelle, Caravan of Dreams, Darkest England, Destination Mecca, Evenings with Idries Shah, Knowing How to Know, Learning How to Learn, Letters and Lectures of Idries Shah, Neglected aspects of Sufi study, Observations, Oriental Magic, Reflections, Seeker after Truth, Special Illumination, Special Problems in the study of Sufi ideas, Sufi thought and action, Tales of the Dervishes, The Dermis Probe, The Elephant in the Dark, The Englishman Handbook, Idries Shah Antology, The Magic Monastery, The natives are restless, wisdom of the Idiots PDF.

Idries Shah 27 Books Collection : A Perfumed Scorpion, A Veiled Gazelle, Caravan of Dreams, Darkest England, Destination Mecca, Evenings with Idries Shah, Knowing How to Know, Learning How to Learn, Letters and Lectures of Idries Shah, Neglected aspects of Sufi study, Observations, Oriental Magic, Reflections, Seeker after Truth, Special Illumination, Special Problems in the study of Sufi ideas, Sufi thought and action, Tales of the Dervishes, The Dermis Probe, The Elephant in the Dark, The Englishman Handbook, Idries Shah Antology, The Magic Monastery, The natives are restless, wisdom of the Idiots PDF.

2022 · PDF