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 ...

Get C++ Multithreading Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.