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.

Re-implement the sample with custom class

Re-implement the sample with custom class - C# Tutorial

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

Re-implement the sample with custom class

- To help understand why multiple threads fail when you're working with the queue class I've created this demonstration. What I did is I went to the Microsoft source code for the queue type, and I copied the subset of the code that's in that class, and then I created my own class here with a different name called Example Queue, and I added that code from Microsoft to the Example Queue, now I didn't use all the code because that makes the demonstration too complex, I just took enough to make the demo work and so you can see why it fails. Before I show you that source code, let's look at the demonstration code. Over here in program cs inside the main method, I have demo one, which will show how to work with the queue in a single thread, and then demo two shows what happens when you work with multiple threads. The code in demo one starts out by instantiating an instance of my custom class, Example Queue, so it's a queue of…

Contents