From the course: Advanced C#: Thread-Safe Data with Concurrent Collections
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Why ICollection and other interfaces are not thread-safe - C# Tutorial
From the course: Advanced C#: Thread-Safe Data with Concurrent Collections
Why ICollection and other interfaces are not thread-safe
- [Instructor] You've heard me say many times in this course that the members, the methods on ConcurrentDictionary are thread safe. But let's dig into this a little bit deeper. Here's what it says in the Microsoft documentation on line 25. "All public and protected members "of ConcurrentDictionary are thread-safe." Now here's the caveat on line 26. "However, members accessed through "one of the interfaces it implements "are not guaranteed to be thread-safe "and may need to be synchronized by the caller." So what this means is the members that were written specifically for ConcurrentDictionary are thread-safe, but the ones coming from the interfaces are not. And there's a lot of interfaces, I'm showing them here. ICollection, there's two IEnumberables, the generic and non-generic version. There's IDictionary, IReadOnlyCollection, and IReadOnlyDictionary. So let's take a look at this one, ICollection. So we'll start by…
Contents
-
-
-
-
-
-
-
-
(Locked)
Review the updated example application1m 34s
-
(Locked)
Potential problems with updating an item3m
-
(Locked)
Use TryUpdate to update dictionary value2m 4s
-
(Locked)
Use a while loop with TryUpdate2m 5s
-
(Locked)
Use the AddOrUpdate method4m 16s
-
(Locked)
Why ICollection and other interfaces are not thread-safe2m 32s
-
(Locked)
Inspect your APIs for ICollection usage2m 40s
-
(Locked)
-
-
-