I read somewhere that the overhead of a mutex is not that much, because the context switching only happens in case of contention.
Also known Futexes in Linux.
Does the same thing hold good in Windows? Is Critical Section a more apt map to mutexes in Linux.
From what i gathered, Critical Sections provide better optimal performance compared to Mutex, is this true for every case?
Is there a corner case where mutexes are faster than critical section in Windows.
Assume only a single process-threads are accessing the mutexes(Just to eliminate the other benefit of Critical Sections)
Added Info: OS windows Server,
Language C++
Considering the specific purpose of
Critical Sections
andMutexes
I don't think you can ask a question regarding the cost as you don't have much alternative when you need multiple threads touching the same data. Obviously, if you just need to increment/decrement a number, you can use theInterlocked*()
functions on avolatile
number and you're good to go. But for anything more complex, you need to use a synchronization object.Start your reading here on the Synchronization Objects available on Windows^. All functions are listed there, nicely grouped and properly explained. Some are Windows 8 only.
As regarding your question,
Critical Sections
are less expensive thanMutexe
s as they are designed to operate in the same process. Read this^ and this^ or just the following quote.I use
Critical Sections
for same process synchronization andMutexes
for cross-process synchronization. Only when I REALLY need to know if a synchronization object was abandoned, I use Mutexes in the same process.So, if you need a sync object, the question is not what are the costs but which is cheaper :) There's really no alternative but memory corruption.
PS: There might be alternatives like the one mentioned in the selected answer here^ but I always go for core platform-specific functionality vs. cross-platformness. It's always faster! So if you use Windows, use the tools of Windows :)
UPDATE
Based on your needs, you might be able to reduce the need of sync objects by trying to do as much self-contained work in a thread as possible and only combine the data at the end or every now and then.
Stupid Example: Take a list of URLs. You need to scrape them and analyze them.
So costs can be lowered by choosing the right tool and thinking how to lower the lock and unlocks. But costs cannot be removed :)
PS: I only think in URLs :)
UPDATE 2:
Had the need in a project to do some measuring. And the results were quite surprising:
std::mutex
is most expensive. (price of cross-platformness)Mutex
is 2x faster thanstd
.Critical Section
is 2x faster than the nativeMutex
.SlimReadWriteLock
is +-10% of theCritical Section
.InterlockedMutex
(spinlock) is 1.25x - 1.75x faster than theCritical Section
.Using std::mutex on windows 8 I usually get 3-4x improvement (on non contending case) speedup by using my own custom made spin lock:
mutex based
});
home made lock free
Tests are made on x86.
I haven't figured out what std::mutex uses underline on windows because it generates a lot of code.