From the course: Threading in C#

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Nested locks

Nested locks - C# Tutorial

From the course: Threading in C#

Start my 1-month free trial

Nested locks

- [Instructor] Now it's time to look into nested locks. Let's start with a very simple lock. We'll call it caztonLock. We can have the main thread acquire the lock, and then do something. Use Control + dot. We can now have an implementation of a method that could do something like this, too. Now, it might seem counterintuitive but this actually is possible to do in a language. In fact, we can do some other work here. That's how we introduce a Task.Delay of 2,000, and then we can also do another lock for the same exact object. Even though it might sound counterintuitive, it's actually possible to run into a situation like that. A lot of times we may not actually realize we are doing that and there's nothing wrong with. For example, this was a method that you wrote, and then there's another method written by another coworker of yours who ended up writing this method and using the lock again. And then there might have been another method right here, which we can call it AnotherMethod…

Contents