1

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.

mczarnek
  • 1,305
  • 2
  • 11
  • 24
  • Mutexes.are a part of the standard library. – n. m. could be an AI Apr 09 '21 at 06:34
  • 1
    I don't think C++ [mutex](https://en.cppreference.com/w/cpp/thread/mutex) work system-wide. They are limited to a process and its threads. – Zoso Apr 09 '21 at 06:41
  • 2
    Does this answer your question? [Monitor vs Mutex](https://stackoverflow.com/questions/38159668/monitor-vs-mutex) or [Making a C++ class a Monitor](https://stackoverflow.com/questions/12647217) – Jérôme Richard Apr 09 '21 at 07:25

3 Answers3

3

C++ Standard does not define system-wide vs per-process primitives. So C++ does not specify whether std::mutex is system-wide.

Reasonable implementations have efficient per-process std::mutex; to have system-wide mutex you'll need to use libraries or operating system objects for your platform

The difference is that per-process mutex may use any memory operations to avoid system calls, as the process memory is shared among process's threads. Atomic operation on that memory are more efficient, and system call is often avoided via them. System-wide mutex will either start with system calls (not efficient), or will have to use shared memory (might be unsafe, also still may have some overhead).

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
2

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.

Werner Erasmus
  • 3,988
  • 17
  • 31
  • 1
    Thank you for all the research, while I appreciate alex's answer you've clearly done a lot more research beyond this and have some extra information people might find handy. Thanks Werner! The one thing from my answer people may want to know: you can force process only mutexes by using official OS specific primitives. – mczarnek Apr 16 '21 at 16:09
1

Update, stumbled across the answer to this while researching something related.

On Windows, Critical Sections can be used for single processes instead of system wide mutexes and are often faster:

Edit: While the above statement is correct, c++ doesn't have the concept system wide mutex. This concept only exists when using OS specific primitives such as win32 CreateMutex and is not relevant to std c++.

Source: std::mutex performance compared to win32 CRITICAL_SECTION

On Linux, pthreads are for processes.

Werner Erasmus
  • 3,988
  • 17
  • 31
mczarnek
  • 1,305
  • 2
  • 11
  • 24
  • This is misleading. You've answered your own question and you've answered it incorrectly. The source that you site shows std::mutex to be faster on more modern version of the VCC compiler. std::mutex in practice is always used for single processes (and as you can see faster at this moment). Also, what does it mean when you state on Linux PThreads are for processes. PThreads is a threading library. Yes, threads run in processes, but PThreads is a threading library that happen to support mutexes. – Werner Erasmus Apr 14 '21 at 03:42
  • You are welcome to write up a new answer that explains this and I'll accept that one instead. The point of the question was that I want to specify mutexes that are specific to the current process only for performance reasons. – mczarnek Apr 15 '21 at 15:21
  • Referring to your link, the answer with the most upvotes states "Please see my updates at the end of the answer, the situation has dramatically changed since Visual Studio 2015.". The problem was not std::mutex, but rather a specific implementation. – Werner Erasmus Apr 16 '21 at 07:05
  • The answer by @Alex Guteniev is as accurate as one can get. In states that the c++ standard doesn't define a system wide concept, and that mutexes for all practical purposes are per process (and therefore according to your needs). – Werner Erasmus Apr 16 '21 at 07:51