Advanced C#: Thread-Safe Data with Concurrent Collections
1h 39mAdvanced2020-05-20
Authors

Walt Ritscher
Programmer, UI Designer, Staff Instructor at LinkedIn Learning
Course details
A "thread-safe" class is one whose members are protected from situations in which one thread interrupts another thread. The standard .NET collection types are not thread safe, which can lead to a slew of problems, including race conditions, data corruption, and unexpected exceptions in modern multithreaded applications. In this course, instructor Walt Ritscher demonstrates how to work with the thread-safe concurrent collections to share data across threads and build more scalable applications. Using practical examples, Walt outlines the problems you can face when working in multithreaded applications and explains why concurrent collections are great at handling multiple threads. He explores how to work with the ConcurrentDictionary class, including how to update data in ConcurrentDictionary. Plus, learn about the producer-consumer pattern and how it relates to concurrent collections types, how to use BlockingCollection—a thread-safe collection class—and more.
Skills covered
C#Programming LanguagesMicrosoftSoftware DevelopmentDeep Dive (X:Y)
Concepts
Introduction
- Thread-safe data with concurrent collections
- What you should know
- How to access the sample code on GitHub
Defining the Problem
- Collections and threads
- Use Queue with single thread
- Use Queue with multiple threads
- Debug the Queue multiple-thread problem
Deconstruct the .NET Queue Class
- Re-implement the sample with custom class
- Examine the ExampleQueue code
Using Thread-Safe Code with Standard Collections
- Rewrite the code to support thread locks
- Use a lock statement and mutex to make thread-safe
- Why locking is not a good solution
- How the concurrent collections are better
Overview of Thread-Safe Collections
- Compare standard and concurrent collections
- Categories of collections
Work with the ConcurrentDictionary Class
- Create a dictionary
- Use TryAdd to add an item
- Use TryRemove to remove an item
- The GetOrAdd method to get or add an item
Updating Data in ConcurrentDictionary
- Review the updated example application
- Potential problems with updating an item
- Use TryUpdate to update dictionary value
- Use a while loop with TryUpdate
- Use the AddOrUpdate method
- Why ICollection and other interfaces are not thread-safe
- Inspect your APIs for ICollection usage
Work with the Producer-Consumer Collections
- Overview of the producer-consumer collections
- Work with ConcurrentQueue
- Work with the ConcurrentStack
- Work with the ConcurrentBag
Using the BlockingCollection
- Understand the BlockingCollection
- Use the BlockingCollection with ConcurrentQueue
- Use the CompleteAdding method
- Use other collections with BlockingCollection
- Read items with multiple consumers
- Create items with multiple producers
Continuing Your Journey
- Next steps
Related courses
- Advanced C# Programming Concepts
- C# Hands-on Practice with Data-Structures
- Advanced C#: Functional Programming Patterns
- Ten Tips for the C# Developer
- C# and .NET Essential Training
- Mastering AI Tools for Efficient C# Development
- Advanced C#: Classes, Records, and Structs
- C# and .NET Development with VS Code