If the application is targeted on Windows Vista or later, could we replace all critical sections with SRW locks? Since critical section is mutually exclusive, for usage it is equivalent to SRW locks in exclusive mode, right? According to MSDN, SRW is optimized both for speed and space. Is there any drawback for doing this? I'm not sure how CS and SRW are implemented internally by Microsoft.
Thanks!
Slim reader/writer (SRW) locks enable the threads of a single process to access shared resources; they are optimized for speed and occupy very little memory. Slim reader-writer locks cannot be shared across processes.
From a theoretical perspective, a critical section is a piece of code that must not be run by multiple threads at once because the code accesses shared resources. A mutex is an algorithm (and sometimes the name of a data structure) that is used to protect critical sections.
See Joe Duffy's book "Concurrent Programming on Windows", pg 289.
The short answer to your question is "almost". There are semantics with recursively acquired CRITICAL_SECTION's that are different for SRWL's. If your program took advantage of these characteristics of Win32's critical sections, then you can't wholesale switch to SRWL easily. You could, however, wrap an SRWL in a wrapper to provide CS like semantics, but there are a bunch of subtleties in doing so, so you are better off sticking to CS's if you are depending on such behavior.
Kenny Kerr's article describes a little about how both are implemented internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With