O'Reilly logo

C# 5.0 in a Nutshell, 5th Edition by Ben Albahari, Joseph Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 22. Advanced Threading

We started Chapter 14 with the basics of threading as a precursor to tasks and asynchrony. Specifically, we showed how to start/configure a thread, and covered essential concepts such as thread pooling, blocking, spinning, and synchronization contexts. We also introduced locking and thread safety, and demonstrated the simplest signaling construct, ManualResetEvent.

This chapter resumes where we left off on the topic of threading. In the first three sections, we flesh out synchronization, locking, and thread safety in greater detail. We then cover:

  • Non-exclusive locking (Semaphore and reader/writer locks)

  • All of the signaling constructs (AutoResetEvent, ManualResetEvent, CountdownEvent and Barrier)

  • Lazy initialization (Lazy<T> and LazyInitializer)

  • Thread-local storage (ThreadStaticAttribute, ThreadLocal<T> and GetData/SetData)

  • Preemptive threading methods (Interrupt, Abort, Suspend and Resume)

  • Timers

Threading is such a vast topic that we’ve put additional material online to complete the picture. Visit http://albahari.com/threading/ for a discussion on the following, more arcane, topics:

  • Monitor.Wait and Monitor.Pulse for specialized signaling scenarios

  • Non-blocking synchronization techniques for micro-optimization (Interlocked, memory barriers, volatile)

  • SpinLock and SpinWait for high-concurrency scenarios

Synchronization Overview

Synchronization is the act of coordinating concurrent actions for a predictable outcome. Synchronization is particularly important when ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required