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.

How the concurrent collections are better

How the concurrent collections are better - C# Tutorial

From the course: Advanced C#: Thread-Safe Data with Concurrent Collections

How the concurrent collections are better

- [Instructor] I think it's instructive to look at the source code for some of these concurrent classes. I'm looking at the ConcurrentDictionary and I found the source code on the Microsoft website at referencesource.microsoft.com. Now, it's not important to know what the code is doing exactly, but I want you to see is there are several places in code where they're using types in the system.threading namespace in order to protect the state inside these methods. So I'm looking at a method called TryAddInternal which is used when you try to add an item to the concurrent dictionary. And you'll see that there's a section down here in 831 where it's using Monitor.Inter, so this locking this section of code. We can see this same thing over here on the TryRemoveInternal. There's a lock statement right here. So the point is, Microsoft's done the work to make sure that all the instant members of these types are thread safe.

Contents