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 and 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 picks up where Chapter 14 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:
Nonexclusive locking (
Semaphoreand reader/writer locks)All of the signaling constructs (
AutoResetEvent,ManualResetEvent,CountdownEvent, andBarrier)Lazy initialization (
Lazy<T>andLazyInitializer)Thread-local storage (
ThreadStaticAttribute,ThreadLocal<T>, andGetData/SetData)Timers
Threading is such a vast topic that we’ve put additional material online to complete the picture. Go online for a discussion on the following, more arcane, topics:
Monitor.WaitandMonitor.Pulsefor specialized signaling scenariosNonblocking synchronization techniques for micro-optimization (
Interlocked, memory barriers,volatile)SpinLockandSpinWaitfor high-concurrency scenarios
Synchronization Overview
Synchronization is the act of coordinating concurrent actions for a predictable outcome. Synchronization is particularly important when multiple threads access the same data; it’s surprisingly easy ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access