Using critical section

Another Windows synchronization object is critical section. Critical section behaves in the same manner as mutex. However, it can be used only in a single process context, as opposed to mutex, which can be shared among multiple processes. However, a critical section object is allocated faster by the system, with much smaller overhead.

Our following example will use the same task as before, only this time, we will use critical section instead of mutex. We can use critical section here because our entire application executes in a single process context.

Getting ready

Make sure Visual Studio is up and running.

How to do it...

Now let's create our program and explain its structure.

  1. Create a new empty C++ Windows application named ...

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.