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.

Work with the ConcurrentStack

Work with the ConcurrentStack

- [Instructor] In this video we're looking at the concurrent stack. And the let me tell you, the code I'm showing you here is nearly identical to the code I showed in the concurrent queue example. The only difference is the type I'm instantiating and the names of the methods I'm calling to add and remove items from the stack. So you see here I instantiate concurrent stack instead of my tasks. Down here I've set up team one instead of team two. I'm adding the robots, but this time I'm using the push method instead of in queue. When I try to peek at the item, the code's identical. This will look at the first item in the stack without removing it. Then I do to a check to see if the stack is empty, and if it's not then I pop an item. So instead of saying try the queue I call try pop and I put the information. And then a for each that iterates over all the items in this stack. And when I run it it'll look the same except…

Contents