I recently posted abut the new Slim R/W Locks [1] introduced with Vista, and how they were vastly more efficient than TMREWS [2].
Apparently, they’re also more efficient than Critical Sections…
Following Up
If you follow the comments in the previous article [1], some extra benchmarks where published, including “degenerate” cases where the Slim R/W Locks are used as exclusive locks.
If you only use the Write lock portion of a R/W Lock, it will behave like an exclusive lock, aka Critical Section.
It appeared that Slim R/W Locks areĀ 1.5 to 2 times faster than Critical Sections.
Wrapping Up
So the advantages of Critical Sections that remain are:
- re-entrance, though code that relies on re-entrance will often be because of bugs/shortcuts, and may be vulnerable to deadlocks as well
- deadlock detection, though this is an IDE debugger feature, and not really automatic
- available on Windows XP and Windows 2003
Both Critical Sections and Slim R/W Locks share the following advantages over custom implementations (like TMonitor)
- efficient relinquishing of CPU time when lock can’t be acquired (rather than hard-coded and hand-tuned spin-lock limits)
- future-proof for past and upcoming CPU architectures (will be updated with the rest of the OS)
- non-Delphi debugging tools support
- low memory footprint
- efficient native implementations exist on other platforms and architectures (cf POSIX)