Hands-On Parallel Programming with C# 8 and .NET Core 3
eBook - ePub

Hands-On Parallel Programming with C# 8 and .NET Core 3

Build solid enterprise software using task parallelism and multithreading

  1. 346 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Parallel Programming with C# 8 and .NET Core 3

Build solid enterprise software using task parallelism and multithreading

Book details
Book preview
Table of contents
Citations

About This Book

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 Kestrel

Book 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 resources

Who 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.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Hands-On Parallel Programming with C# 8 and .NET Core 3 by Shakti Tanwar in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Languages. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789133615
Edition
1

Section 1: Fundamentals of Threading, Multitasking, and Asynchrony

In this section, you will become familiar with the concepts of threading, multitasking, and asynchronous programming.
This section comprises the following chapters:
  • Chapter 1, Introduction to Parallel Programming
  • Chapter 2, Task Parallelism
  • Chapter 3, Implementing Data Parallelism
  • Chapter 4, Using PLINQ

Introduction to Parallel Programming

Parallel programming has been supported in .NET since the start and it has gained a strong footing since the introduction of the Task Parallel Library (TPL) from .NET framework 4.0 onward.
Multithreading is a subset of parallel programming and is one of the least understood aspects of programming; it's one that many new developers struggle to understand. C# has evolved significantly since its inception. It has very strong support, not only for multithreading but also for asynchronous programming. Multithreading in C# goes way back to C# version 1.0. C# is primarily synchronous, but with the strong async support that has been added from C# 5.0 onward, it has become the first choice for application programmers. Whereas multithreading only deals with how to parallelize within processes, parallel programming also deals with inter-process communication scenarios.
Prior to the introduction of the TPL, we relied on Thread, BackgroundWorker, and ThreadPool to provide us with multithreading capabilities. At the time of C# v1.0, it relied on threads to split up work and free up the user interface (UI), thereby allowing the user to develop responsive applications. This model is now referred to as classic threading. With time, this model made way for another model of programming, called TPL, which relies on tasks and still uses threads internally.
In this chapter, we will learn about various concepts that will help you learn about writing multithreaded code from scratch.
We will cover the following topics:
  • Basic concepts of multi-core computing, starting with an introduction to the concepts and processes related to the operating system (OS)
  • Threads and the difference between multithreading and multitasking
  • Advantages and disadvantages of writing parallel code and scenarios in which parallel programming is useful

Technical requirements

All the examples demonstrated in this book have been created in Visual Studio 2019 using C# 8. All the source code can be found on GitHub at https://github.com/PacktPublishing/Hands-On-Parallel-Programming-with-C-8-and-.NET-Core-3/tree/master/Chapter01.

Preparing for multi-core computing

In this section, we will introduce the core concepts of the OS, starting with the process, which is where threads live and run. Then, we will consider how multitasking evolved with the introduction of hardware capabilities, which make parallel programming possible. After that, we will try to understand the different ways of creating a thread with code.

Processes

In layman's terms, the word process refers to a program in execution. In terms of the OS, however, a process is an address space in the memory. Every application, whether it is a Windows, web, or mobile application, needs processes to run. Processes provide security for programs against other programs that run on the same system so that data that's allocated to one cannot be accidentally accessed by another. They also provide isolation so that programs can be started and stopped independently of each other and independently of the underlying OS.

Some more information about the OS

The performance of applications largely depends on the quality and configuration of the hardware. This includes the following:
  • CPU speed
  • Amount of RAM
  • Hard disk speed (5400/7200 RPM)
  • Disk type, that is, HDD or SSD
Over the last few decades, we have seen huge jumps in hardware technology. For example, microprocessors used to have a single core, which is a chip with one central processing unit (CPU). By the turn of the century, we saw the advent of multi-core processors, which are chips with two or more processors, each with its own cache.

Multitasking

Multitasking refers to the ability of a computer system to run more than one process (application) at a time. The number of processes that can be run by a system is directly proportional to the number of cores in that system. Therefore, a single-core processor can only run one task at a time, a dual-core processor can run two tasks at a time, and a quad-core processor can run four tasks at a time. If we add the concept of CPU scheduling to this, we can see that the CPU runs more applications at a time by scheduling or switching them based on CPU scheduling algorithms.

Hyper-threading

Hyper-threading (HT) technology is a proprietary technology that was developed by Intel that improves the parallelization of computations that are performed on x86 processors. It was first introduced in Xeon server processors in 2002. HT-enabled single-processor chips run with two virtual (logical) cores and are capable of executing two tasks at a time. The following diagram shows the difference between single- and multi-core chips:
The following are a few examples of processor configurations and the number of tasks that they can perform:
  • A single processor with a single-core chip: One task at a time
  • A single processor with an HT-enabled single-core chip: Two tasks at a time
  • A single processor with a dual-core chip: Two tasks at a time
  • A single processor with an HT-enabled dual-core chip: Four tasks at a time
  • A single processor with a quad-core chip: Four tasks at a time
  • A single processor with an HT-enabled quad-core chip: Eight tasks at a time
The following is a screenshot of a CPU resource monitor for an HT-enabled quad-core processor system. On the right-hand side, you can see that there are eight available CPUs:
You might be wondering how much you can improve the performance of your computer simply by moving from a single-core to a multi-core processor. At the time of writing, most of the fastest supercomputers are built on the Multiple Instruction, Multiple Data (MIMD) architecture, which was one of the classifications of computer architecture proposed by Michael J. Flynn in 1966.
Let's try to understand this classification.

Flynn's taxonomy

Flynn classified computer architectures into four categories based on the number of concurrent instruction (or control) streams and data streams:
  • Single Instruction, Single Data (SISD): In this model, there is a single control unit and a single instruction stream. These syste...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. About Packt
  5. Contributors
  6. Preface
  7. Section 1: Fundamentals of Threading, Multitasking, and Asynchrony
  8. Introduction to Parallel Programming
  9. Task Parallelism
  10. Implementing Data Parallelism
  11. Using PLINQ
  12. Section 2: Data Structures that Support Parallelism in .NET Core
  13. Synchronization Primitives
  14. Using Concurrent Collections
  15. Improving Performance with Lazy Initialization
  16. Section 3: Asynchronous Programming Using C#
  17. Introduction to Asynchronous Programming
  18. Async, Await, and Task-Based Asynchronous Programming Basics
  19. Section 4: Debugging, Diagnostics, and Unit Testing for Async Code
  20. Debugging Tasks Using Visual Studio
  21. Writing Unit Test Cases for Parallel and Asynchronous Code
  22. Section 5: Parallel Programming Feature Additions to .NET Core
  23. IIS and Kestrel in ASP.NET Core
  24. Patterns in Parallel Programming
  25. Distributed Memory Management
  26. Assessments
  27. Other Books You May Enjoy