Switching to System.Threading.Lock Did Not Speed Up Uncontended Locks
A developer's code reviewer suggested replacing lock(object) with the newer System.Threading.Lock type, which is claimed to be faster. Since .NET 9 and C# 13, the lock statement compiles to a scope-based EnterScope() call on Lock instances instead of Monitor.Enter/Exit. To test this, the author ran a single-threaded, uncontended benchmark performing 50 million lock acquire-and-release cycles while incrementing a counter. The measurements did not show the expected improvement over the classic object-based lock. The author is curious whether higher-core hardware under real contention would produce different results.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in