Concurrency In C# Cookbook - Asynchronous, Parallel, and Multithreaded Programming

For uninitialized people, concurrent and multithreading is not desirable. People make well-behaved code run in the most horrible ways. Race conditions and the like cause huge crashes (which always seems to happen, both in production and during demos). Some even declare that “threads are evil” and avoid concurrency altogether. A few developers have experimented with concurrency and used it fearlessly; But most developers have been hurt by concurrency in the past, and the experience leaves a bad taste in their mouth.

However, concurrency is fast becoming a requirement for modern applications. In addition, now that users expect fully responsive interfaces, server applications must scale to unprecedented levels. Concurrency addresses both trends.

Fortunately, many modern libraries make concurrency much easier! Parallel processing and asynchronous programming are no longer the exclusive areas of the wizard. For each developer, these libraries make responsive and scalable application development the realistic goals via raising the abstraction level. If you’ve had a problematic concurrency situation in the past, I encourage you to give it another try using modern tools. We may never be able to say concurrency is easy, but it’s certainly not as hard as it used to be!

About This Book

This book is written for developers who want to learn about modern concurrency methods. I assume you have quite a little .Net experience, including understanding generic collections, enumerable objects, and LINQ. I don’t expect you to have any knowledge of multithreading or asynchronous programming. If you have some experience in these areas, you may still find this book helpful because it introduces the latest, more reliable, and more accessible libraries to use.

Concurrency is helpful for any application. Whether you’re working on a desktop, mobile, or server application; Now, concurrency is a requirement across the board. You can use the methods in this book to improve the responsiveness of the user interface and the scalability of the server. We have reached the point where concurrency is ubiquitous, and professional developers need to understand these technologies and their use.

Concurrency

Concurrency is a crucial aspect of beautiful software. For decades, concurrency was possible but challenging to achieve. Concurrent software is hard to write, hard to debug, and hard to maintain. As a result, many developers choose the more straightforward path and avoid concurrency. There is modernity. With libraries and language features available for.NET applications, concurrency is now much more straightforward. Microsoft has led the way by dramatically lowering the bar for concurrency. Previously, only experts can do parallel programming; Now, concurrency programming belongs to every developer.

Asynchronous Programming

Asynchronous programming has two main benefits. The first benefit is for end-user GUI programs: asynchronous programming supports responsiveness. For example, everyone has experienced the temporary lock while a program is working; Asynchronous programs can resolve this: they remain responsive to user input while working. The second benefit is server-side programming: asynchronous programming supports scalability. For example, using thread pools can scale server applications, and however, asynchronous programming can typically scale server applications an order of magnitude better than thread pools.

Asynchronous programming has two advantages that stem from the same essential aspect: asynchronous programming frees up a thread. Asynchronous programming doesn’t block the UI thread, allowing GUI applications to continue responsive to user input. For server applications, asynchronous programming releases the request thread; This allows the server to use its threads to service additional requests.

Parallel Programming

Parallel programming should be used when you have a large amount of computational work that can be split into separate chunks. Parallel programming will temporarily increase the use of the CPU, in order to improve the throughput; The CPU, often free on the client system, is ideal, but usually not on the server system.

Most servers have some built-in parallelism; For example, ASP. NET will process multiple requests in parallel. Thus, it may still be useful in some cases with parallel programming on a server  (if you know that concurrent users are always low). Still, more often than not, parallel programming on a server affects its built-in parallelism and therefore does not provide any real benefit.

Parallelism comes in two forms: data parallelism and task parallelism. Data parallelism means that when you have many data items to work with, each block of data is processed essentially independently of the other blocks. Task parallelism is when you have a bunch of work to do, each of which is essentially independent of the others. Task parallelism can be dynamic; If results from a job in several additional tasks, the system will add those results to the work pool.

Views: 63

Leave a Reply

Your email address will not be published. Required fields are marked *