Comparison of Win32 CMutex and the standard library std :: mutex

Since the advent of the thread library in C ++ 11, I have been reviewing my code, making some changes to divert it from multi-threaded code of a specific platform to portable standard library code.

However, I am intrigued whether there is a difference in performance or functionality between the standard library std::mutex and std::lock_guard<std::mutex> and Win32-specific CMutex and CSingleLock .

I have no experience profiling multi-threaded code, and I don’t know the internal components of either of the two mutex classes, so I couldn’t even be afraid of guessing.

+4
source share
1 answer

The functional relationship is confident - CMutex maps directly to the Mutex type of Win32, while std::mutex is more basic, probably implemented using win32 CRITICAL_SECTION , removing the recursive character and std::recursive_mutex wrapping CRITICAL_SECTION . They will work similarly to CCriticalSection .

CMutex is a heavyweight that is used in practice to create named mutexes for interprocess communication. You should not use its in-process.

If your question is to compare recursive_mutex vs CCriticalSection , I would put on almost the same performance. The CSingleLock interface has a full braindead interface (it takes a second argument, which defaults to FALSE instead of TRUE ), so in practice I have never used it directly through a macro only to avoid error.

In the new code, I first tried to solve everything using std::future , and a violin with locks only as a last resort. C ++ 11 threads make sense to use, so until you need CMultiLock functionality, it's better. I have not yet researched how to cover the latter case, but would be surprised if it is not easy to do.

+5
source

Source: https://habr.com/ru/post/1483764/


All Articles