Chapter 5. Coordination Data Structures

WHAT'S IN THIS CHAPTER?

  • Understanding lightweight coordination data structures

  • Working with lightweight synchronization primitives

  • Choosing the most appropriate synchronization primitive according to your needs

  • Synchronizing concurrent tasks with barriers

  • Working with timeouts and cancellation tokens combined with the synchronization primitives

  • Working with mutual-exclusion locks and critical sections

  • Refactoring code to avoid locks

  • Working with spin-based waiting

  • Understanding spinning and yielding

  • Working with lightweight manual reset events

  • Limiting concurrency to access a resource with a semaphore

  • Understanding and preventing synchronization-related bugs

  • Working with atomic operations

This chapter is about synchronizing the work performed by diverse concurrent tasks. The chapter covers some classic synchronization primitives and the new lightweight coordination data structures introduced by .NET Framework 4. It is important to learn the different alternatives, so that you can choose the most appropriate one for each concurrency scenario that requires communication and/or synchronization between multiple tasks. This way, you are going to be able to implement more complex algorithms and to solve potential bugs when the designs don't consider the problems associated with heavyweight synchronization mechanisms.

USING CARS AND LANES TO UNDERSTAND THE CONCURRENCY NIGHTMARES

Figure 5-1 shows one car in a single lane. There are three black flags and two gray ...

Get Professional Parallel Programming with C#: Master Parallel Extensions With .NET 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.