Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace critical section with SRW lock

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!

like image 641
Crend King Avatar asked Aug 17 '10 02:08

Crend King


People also ask

What is SRW lock?

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.

What is the difference between mutex and critical section?

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.


1 Answers

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.

like image 167
Mark oskin Avatar answered Oct 22 '22 23:10

Mark oskin