Search code examples
c++performancemutexmonitor

How to create a single process mutex within C++?


So I'm reading about monitors vs mutexes and finding mentions that suggest that monitors are faster mutexes because they don't lock system wide but rather only across the threads of a given process.

Is there some way in C++ to accomplish or simulate this?

Edit: I'm curious now what the difference is between system wide mutex and one restricted to a specific process.


Solution

  • The answer by @Alex Guteniev is as accurate as one can get (and should be considered the accepted answer). It states that the c++ standard doesn't define a system wide concept, and that mutexes for all practical purposes are per process i.e for synchronization between threads (execution agents) in a single process (and therefore according to your needs). The C++ makes it clear what a thread (std::thread) is (33.3 - ... intended to map one-to-one with OS threads (in my draft, at least...N4687)). Microsoft post VC2015 has improved their implementation to use windows primitives as stated here. This is also indicated here in the most upvoted answer. I've also looked at the boost library implementations (which often precedes/influences the c++ standard) for microsoft and (AFAICT) it doesn't use any inter-process calls.

    So to answer your question. In C++ threads and monitors are practically the same thing if this definition is to be considered accurate.