TMonitor versus Critical sections revisited: when possible, use SRWL over TMonitor, then TCritical section
Posted by jpluimers on 2020/08/19
Via [WayBack] TMonitor vrs Critical sections revisited. Back then (https://www.delphitools.info/2013/06/06/tmonitor-vs-trtlcriticalsection/), it was found that critic… – Kiriakos Vlahos – Google+ as he re-did the measurements that DelphiTools did before Delphi XE5 came out:
Slim Reader/Writer Lock (SWRL) is even faster than TMonitor:
- CriticalSection 0.035
- TMonitor 0.019
- SWRL 0.012
Of course if you have a speedy lock-free solution, that is always favourable, but few people know how to write lock free data structures or how to find the libraries (despite Julian Bucknall covering quite a few of them in Delphi back in the days).
Related:
- [WayBack] TMonitor vs TRTLCriticalSection – DelphiTools
- Fixed in XE5 by doing a
TSpinWait.SpinCycle
instead ofYieldProcessor
: [WayBack] The Oracle at Delphi: Monitoring the Monitor (the underlying function got introduced back in [Archive.is] SyncObjs.TSpinWait.SpinCycle – XE API Documentation) - [WayBack] Slim Reader/Writer (SRW) Locks (Windows)
–jeroen
Alexandre Machado said
The biggest problem with SRW lock is that it is not fair. In cases where you have lots of readers and a single writer, the writer may not have a chance to hold the lock because there is always one or more readers. The Critical section doesn’t help much either. The best option is indeed going lock free.
jpluimers said
I like your point of view.
Thanks for commenting it.
Stefan Glienke said
When possible, don’t use any locking at all ;)
jpluimers said
So true!