Learn how to use concurrent collections in multithreaded code! This course will teach you the correct use of ConcurrentDictionary, as well as introducing you to producer-consumer scenarios and the blocking collection.
Learn how to use concurrent collections in multithreaded code! This course is a comprehensive introduction to the concurrent collections. It shows you how to use each of the main collection types: ConcurrentDictionary, ConcurrentQueue, ConcurrentBag, and ConcurrentStack. You'll learn the correct techniques for using these collections to avoid bugs such as race conditions, and also how to use BlockingCollection with the concurrent collections correctly in producer-consumer scenarios. The course rounds off with a look at some concurrent collection best practices.
Simon is a senior developer with over 20 years programming experience, particularly covering C++ and the .NET Framework. He believes knowledge is to be shared and has written or co-written more than a dozen books for professional programmers.
Introducing the Concurrent Collections Hello. I'm Simon Robinson welcoming you to this Pluralsight course, C# Concurrent Collections. The concurrent collections are a new set of collection types that Microsoft introduced for. NET 4, which is specifically designed to be used in a multi-threaded environment. The aim of this course is to teach you how to use the concurrent collections safely and correctly and this looks like the course all ready to open, let's look inside. In the box we have the course modules. I've arranged them to show how they depend on each other. This first module is a general overview. I'll mention the purposes of the different types available in the system. collections. concurrent namespace and clarify in what ways concurrent collections can and can't help in a multi-threaded environment. In particular, you will learn the importance of the ConcurrentDictionary as the main general purpose concurrent collection. My teaching style, by the way, is very much that I like to make sure we've covered the concepts adequately and then apply them, which means there's a fair bit of theoretical learning in this first module, and then much more actual coding as we work through the course. The next two modules will go into ConcurrentDictionary in some detail. In module two I'll show you what methods it provides and how it differs from the standard generic dictionary using very simple code. Then in module three we will apply this knowledge using ConcurrentDictionary in a much more realistic, multi-threaded demo. Then we do basically the same thing for a group of collections that I personally like to call the Producer-Consumer collections, concurrent queue, concurrent stack, and concurrent bag. Again, there are two modules, module four introduces these tops, and then module five shows a more realistic demo, which also brings in the BlockingCollection, and we will wind up in the last module with a look at some best practices. Here you will learn a few tips on using concurrent collections effectively and learn how to avoid some common pitfalls, especially related to performance. Now before we begin I just need to mention a couple of prerequisites for the course. Firstly, this course is about concurrent collections, it's not a general threading or thread safety course. I will draw attention to particular points of thread safety and thread synchronization that relate to concurrent collections, but in general I'm assuming you know what a thread is, and you're comfortable, for example, using. NET tasks to do parallel processing. If you find while you're watching this course that you do need a refresher in the threads and tasks I can recommend Joe Hummel's Pluralsight course, Introduction to Async and Parallel Programming in. NET 4. Also, and just as important, I'm assuming you know how to use the standard generic collections, which roughly means the ones in the system. collections. generic namespace. My previous course, C# Collections Fundamentals covers the standard collections, and this concurrent collections course that you're watching now is largely a direct follow on from that course, so if you feel you need a refresher in standard collections do check that out. As far as code goes, I'm using Visual Studio 2013 and. NET 4. 5 for all the demos. Concurrent collections were actually released with. NET 4. 0, so if you're using. net4. 0 and Visual Studio 2010 you should still be able to use the concurrent collections. Bear in mind though that there were originally some performance issues with concurrent back in 4. 0, those were fixed in. NET 4. 5 and are no longer an issue.
Concurrent Dictionary Demo Hello. I'm Simon Robinson and this is the C# Concurrent Collections course. This module is called ConcurrentDictionary Demo and its purpose is to apply the knowledge we picked up in the last module by showing you how to use a ConcurrentDictionary correctly in a realistic sample. The sample will be based on an app that has multiple threads updating stock levels for the Famous TechieSimon Perfect Geek Clothing Company. We'll see in particular how AddOrUpdate lets us update the stock levels correctly, even when we don't know what other threads are doing at the time or what the stock level is at the time we try to update it. We'll also see that we need to take a lot of care with providing delegates or lambdas to ConcurrentDictionary methods because of the issue that delegates might be executed more than once, but we'll also see how using a ConcurrentDictionary carefully can allow you to write thread-safe code without explicitly using any locks, which I think is pretty neat. Let's begin.
The Producer-Consumer Collections: Queues, Stacks and Bags Hello. This is Simon Robinson and you're watching the Pluralsight course, C# Concurrent Collections. This module is called The Producer-Consumer Collections: Queues, Stacks, and Bags. It's quite a short module, which will introduce you to the three remaining concurrent collections that we have yet to look at and, of course, that means ConcurrentQueue, ConcurrentStack, and ConcurrentBag. I'll start by showing you how the methods exposed by ConcurrentQueue differ from those provided by the standard single-threaded queue. Next, we'll look at how ConcurrentStack and ConcurrentBag compare with ConcurrentQueue. This will show up the fact that these types return elements in a different order, but apart from that they have essentially identical functionality. I'll explain what a producer-consumer scenario is and you'll learn why I like to refer to these types as producer-consumer collections and finally, I'll mention how the IProducerConsumerCollection of T interface captures the essence of a producer-consumer collection. This is a somewhat theoretical module. I'll be using very simple code to show what these types can do and how they are related, but I'm basically giving you background knowledge, and it'll be in the next module, Producer-Consumer and BlockingCollection demo, where you'll learn how to apply the knowledge, so let's begin. Just to remind you how these types are related I've once again, put up the slide from the first module that shows what types are available in the System. Collections. Concurrent namespace. The types we're covering in this module are in bold and, not surprisingly, are all in the group that I've labeled Producer-consumer.
Producer-consumer and BlockingCollection Demo Hello. This is Simon Robinson and you're watching the Pluralsight course, C# Concurrent Collections. This module is called Producer-consumer and BlockingCollection Demo and it focuses on a realistic demo. We'll learn how you can use ConcurrentQueue in a scenario in which some threads are producing tasks to be processed and other threads are consuming those tasks. You'll then see that ConcurrentQueue isn't quite adequate in this scenario because it leaves you needing to keep polling for tasks if the queue is empty, and this will lead us to the final type we need to learn about, the BlockingCollection. You'll learn how the BlockingCollection encapsulates any producer-consumer collection and provides additional services, including the ability to wait until an item becomes available in the collection.
Some Concurrent Collections Best Practices Hello, I'm Simon Robinson and this is the final module of the Global Sat course C# Concurrent Collections. In this module I'm going to review a couple of best practices to remember when using Concurrent Collections. In particular, I'll point out that it's best to have threads access your concurrent collections sparingly otherwise you'll hit performance and scalability problems. I'll draw attention to certain methods and properties, for example, count' and is empty on concurrentdictionary and the concurrentbag, which can be much slower than you might expect. I'll review the behavior of concurrent collection enumerators which is also not what you might expect. And I'll take a quick look at precisely when you need to use concurrent collections, and we'll see there is one particular multi-threaded situation where you can safely stick to the standard collections. I should stress, by the way, the 'some' in the title of this module: Some Concurrent Best Practices. This module isn't intended to give you a comprehensive list of every possible best practice, it's more, by nature, a quick run through of a couple of issues that are important and which you might not expect if you're new to concurrent collections and which I think you should know to round off your understanding of the topic. Okay, let's begin.