Chapter 8. Concurrent and Parallel Programming under .NET

So far, we have been mostly focusing on the GoF design patterns. When the catalog appeared, the computing world was mostly sequential, and the bias was reflected in the catalog. The world has changed a lot since the publication of the catalog in 1994. There was a shift towards language-level concurrency and parallelism due to the arrival of the Java and C# programming languages. The processor designers understood the limits of constructing powerful single-core CPUs, and began focusing on many core CPUs. This brought its own set of complexity in developing software using the existing paradigms. Even a language like C++, which relegated concurrency to libraries, added language-level concurrency features in its latest avatar, C++ 11/14. With the advent of functional programming features into the OOP world, the programming landscape changed drastically. In this chapter, you will learn some techniques for writing concurrent and parallel programs using the C# programming language and .NET platform features. In this chapter, we will cover the following points:

  • Factors that influenced evolution of concurrency and parallelization models
  • Concurrency versus Parallelism
  • .NET Parallel Programming libraries
  • Some Parallel/Concurrent memes/idioms
    • Embarrassingly parallel
    • Fork/join parallelism
    • Producer/consumer paradigm

Days of Free Lunch

The celebrated Moore's law went on despite microprocessors hitting the clock-speed limits in terms of heat dissipation and achieving fabrications beyond nano-level. CPU designers found an intelligent workaround for this roadblock-that of leveraging the increased chip densities to scale the computation infrastructure horizontally as opposed to the traditional vertical way. This principle has deep consequences in modern day architecture and design, where application scalability (both vertical and horizontal) inherits natural elasticity with respect to the infrastructure that powers them. What resulted was a new generation of CPU architectures including hyper-threading, multi-core, and many-core. It wasn't too late before the developers realized that the Free Lunch (just leveraging the conventional CPU performance gains through clock speed, execution optimization, and cache, with no change in their programs) was over. Herb Sutter wrote an influential article in Dr. Dobb's Journal about this, aptly titled, The Free Lunch Is Over: A Fundamental Turn toward Concurrency in Software!

Note

This realization that software was the gating factor in terms of achieving more with most of what was available was the dawn of a new revolution in software architecture and design.

This new model helped remove many shortcomings in modeling real-world problem scenarios. It seemed though that we (developers) began adding justice to the grand concurrent design that went into modelling this world with interactions to be parallel as opposed to just being sequential and object-oriented. The prevalent parallelization techniques (leveraged mostly by the scientific community for doing embarrassingly parallel computations)-that of explicit threading with an affinity to hardware threads-was primitive and less scalable. Very few people had expertise (or rather felt comfortable) in working with the low-level constructs for leveraging multi-threading in their applications. This state of affairs brought in a strong imperative for creating better abstractions and the needed APIs to write the next generation software that would inherently leverage concurrency and parallelism to achieve more with most of what was available.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.72