Replace critical area with SRW lock

If the application is for Windows Vista or later, can we replace all critical sections with SRW Lock ? Since the critical section is mutually exclusive, to use it is equivalent to locking SRW in exclusive mode, right? According to MSDN, SRW is optimized for both speed and space. Is there a flaw in this? I do not know how CS and SRW are implemented by Microsoft.

Thanks!

+6
multithreading synchronization critical-section slim
source share
1 answer

See Joe Duffy's book, Parallel Programming on Windows, page 289.

The short answer to your question is "almost." There is semantics with recursively acquired CRITICAL_SECTION, which differ for SRWL. If your program has taken advantage of these characteristics of critical Win32 partitions, then you cannot easily switch to SRWL. However, you could wrap SRWL in a wrapper to provide CS as semantics, but there are tons of subtleties, so you'd better stick with CS if you are dependent on this behavior.

Kenny Kerr 's article describes a little about how both are implemented internally.

+7
source share

All Articles