O'Reilly logo

C++ Multithreading Cookbook by Milos Ljumovic

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Locking and avoiding deadlocks

In our previous example, we had the bContinue variable that both threads were accessing without any restrictions on which thread can read or write in it. Well, that's usually application bottleneck and should be avoided. The Microsoft .NET framework provides us with mechanisms to avoid situations like that with very little difficulty.

Our following example will implement another class, Lock, similar to the previous one that we used in Chapter 3, Managing Threads. The Lock class will help us to stop one thread from reading while the other one is writing and vice versa. Unlike the C# language, which provides the lock operator, C++ doesn't. That's why we will implement our own lock using the Monitor object to lock the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required