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.

Why locking is not a good solution

Why locking is not a good solution

- [Instructor] We have successfully locked the code here in Make Robot and up in the static void main method I've used the lock statement here and the new text in static void main and now we don't have to worry about the shared state issues anymore or calling these non thread safe methods. But this is not the optimal choice and I'll give you a couple reasons why. First example is that this lock statement essentially turns my multi-threaded code back into a single threaded code application. At least that's true within this lock statement. Now think about why you write multi-threaded applications in the first place. Typically we do that for a lot of reasons. One is to create a more responsive UI in a desktop application. Maybe you want more throughput in your code or you want to do parallel processing. Those are all good reasons to use multi-threaded code. And what we've done here is basically defeated those ideals…

Contents