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

Introduction


In the previous chapters, we discussed several ways to create threads and organize their cooperation. Now, let's consider another scenario where we will create many asynchronous operations that take very little time to complete. As we discussed in the Introduction section of Chapter 1, Threading Basics, creating a thread is an expensive operation, so doing this for each short-lived, asynchronous operation will include a significant overhead expense.

To deal with this problem, there is a common approach called pooling that can be successfully applied to any situation when we need many short-lived, expensive resources. We allocate a certain amount of these resources in advance and organize them into a resource pool. Each time we need a new resource, we just take it from the pool, instead of creating a new one, and return it to the pool after the resource is no longer needed.

The .NET thread pool is an implementation of this concept. It is accessible via the System.Threading.ThreadPool...