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.
Figure 5-1 shows one car in a single lane. There are three black flags and two gray ...