Menu

When lock no longer means Monitor
📰

When lock no longer means Monitor

DEV Community: csharp·Zero Heartbeat·8 days ago
#VJk06D2t
#devtocsharp#devto#dev#lock#system
Long article (1344 words) — no AI summary yet.
Reading 0:00
15s threshold

For twenty years, lock (x) meant exactly one thing in IL: Monitor.Enter , a try , and a guarded Monitor.Exit . You could read the pattern in your sleep. C# 13 quietly broke that reflex. Lock over the new System.Threading.Lock type no longer calls Monitor at all — it calls EnterScope() and disposes a ref struct in a finally . The source still reads lock (x) . The IL is a different shape entirely, and a decompiler that has not learned the new shape will either miss the lock or print a using that was never there.

The real question

The syntax did not change. Both of these are legal, and both read lock (_gate) { ... } :

private readonly object _gate = new ( ) ; // classic private readonly System . Threading . Lock _lock = new ( ) ; // C# 13

Enter fullscreen mode

Exit fullscreen mode

The compiler looks at the static type of the operand. If it is System.Threading.Lock , it emits the new scope-based lowering.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.