<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52" -->

---
title: Locking and Concurrency Control in .NET 10 | daily.dev
description: A walkthrough of concurrency control mechanisms in .NET 10, starting from a classic race condition in a bank account balance update. Covers the Lock statement...
canonical: https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Locking and Concurrency Control in .NET 10 | daily.dev
og:description: A walkthrough of concurrency control mechanisms in .NET 10, starting from a classic race condition in a bank account balance update. Covers the Lock statement...
og:url: https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52
og:image: https://api.daily.dev/og/posts/uM1RhZp52.png
og:image:alt: Locking and Concurrency Control in .NET 10
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Locking and Concurrency Control in .NET 10

**[Waseem .NET Newsletter](https://daily.dev/sources/mwaseemzakir)** · 8 min read · 14 upvotes · 0 comments

## Summary

A walkthrough of concurrency control mechanisms in .NET 10, starting from a classic race condition in a bank account balance update. Covers the Lock statement (including the new System.Threading.Lock type from .NET 9), SemaphoreSlim for limiting concurrent async operations, Interlocked for atomic operations on simple values, and ConcurrentDictionary for thread-safe collections, including a warning about AddOrUpdate delegates running multiple times. Also touches on ReaderWriterLockSlim, Monitor, Mutex, and Channel<T> for producer-consumer scenarios. Closes by explaining that application-level locks don't protect against conflicts across multiple server instances, and shows how EF Core's rowversion concurrency token handles database-level concurrency conflicts via DbUpdateConcurrencyException.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://mwaseemzakir.substack.com/p/locking-and-concurrency-control-in>

## Questions this post answers

### When should I use SemaphoreSlim instead of the lock statement in C#?

Use SemaphoreSlim when the protected operation contains an await, since C# does not allow await inside a lock block. SemaphoreSlim's WaitAsync lets a caller wait without blocking a thread, and it can also limit concurrency to a specific number of simultaneous operations, such as capping concurrent external API calls to three using new SemaphoreSlim(3, 3).

_Sorting out lock versus SemaphoreSlim in async C# code is easier with concurrency deep-dives like this on daily.dev._

### Why does ConcurrentDictionary AddOrUpdate cause bugs with side effects like sending an email?

The update delegate passed to AddOrUpdate can execute multiple times when several threads update the same key concurrently, because it runs outside the dictionary's internal locks. Using that delegate to send an email, publish an event, or perform any operation that must happen exactly once can trigger duplicate side effects, so the delegate should only calculate values.

_Developers hardening thread-safe collections can dig into pitfalls like this via daily.dev._

### Does a C# lock statement prevent race conditions when an API runs on multiple servers?

No, an in-process lock only coordinates threads sharing the same memory and does nothing across separate server instances. If two requests hit different servers, each acquires its own local lock and both can update the same database record simultaneously; database-level concurrency control, such as EF Core's rowversion token that throws DbUpdateConcurrencyException on conflicting writes, is needed instead.

_Teams scaling APIs across multiple instances can compare application locks versus database concurrency tokens on daily.dev._

## Similar posts on daily.dev

- [Understanding Race Conditions in ASP.NET Core](https://daily.dev/posts/understanding-race-conditions-in-asp-net-core-srb3ao9gq) · Telerik · 8 upvotes · 0 comments
- [Lost Updates in EF Core: Optimistic vs. Pessimistic Concurrency](https://daily.dev/posts/lost-updates-in-ef-core-optimistic-vs-pessimistic-concurrency-xmi3kzmrw) · Atomic Spin · 0 upvotes · 0 comments
- [.NET Developer, Personal Blog](https://daily.dev/posts/net-developer-personal-blog-hvxdh2qgl) · Laszlo Deak · 0 upvotes · 0 comments
- [Async or Swim: The Complete Guide to Asynchronous .NET](https://daily.dev/posts/async-or-swim-the-complete-guide-to-asynchronous-net-n2fszfnf6) · ITNEXT · 9 upvotes · 1 comments
- [The Staggering Complexity And Subtlety Of Concurrency](https://daily.dev/posts/the-staggering-complexity-and-subtlety-of-concurrency-e4mpqhp2q) · Hackaday · 1 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Locking and Concurrency Control in .NET 10","url":"https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52"},"datePublished":"2026-09-26T07:43:22.637Z","dateModified":"2026-09-26T07:43:47.442Z","description":"A walkthrough of concurrency control mechanisms in .NET 10, starting from a classic race condition in a bank account balance update. Covers the Lock statement...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7884181a963fe7896e415f28009da547?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7884181a963fe7896e415f28009da547?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Waseem .NET Newsletter","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Waseem .NET Newsletter","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/a10ec420048a48bf86f6889f2de88180","url":"https://daily.dev/sources/mwaseemzakir"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":14},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Waseem .NET Newsletter","item":"https://daily.dev/sources/mwaseemzakir"},{"@type":"ListItem","position":3,"name":"Locking and Concurrency Control in .NET 10"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52#faq","mainEntity":[{"@type":"Question","name":"When should I use SemaphoreSlim instead of the lock statement in C#?","acceptedAnswer":{"@type":"Answer","text":"Use SemaphoreSlim when the protected operation contains an await, since C# does not allow await inside a lock block. SemaphoreSlim's WaitAsync lets a caller wait without blocking a thread, and it can also limit concurrency to a specific number of simultaneous operations, such as capping concurrent external API calls to three using new SemaphoreSlim(3, 3). Sorting out lock versus SemaphoreSlim in async C# code is easier with concurrency deep-dives like this on daily.dev."}},{"@type":"Question","name":"Why does ConcurrentDictionary AddOrUpdate cause bugs with side effects like sending an email?","acceptedAnswer":{"@type":"Answer","text":"The update delegate passed to AddOrUpdate can execute multiple times when several threads update the same key concurrently, because it runs outside the dictionary's internal locks. Using that delegate to send an email, publish an event, or perform any operation that must happen exactly once can trigger duplicate side effects, so the delegate should only calculate values. Developers hardening thread-safe collections can dig into pitfalls like this via daily.dev."}},{"@type":"Question","name":"Does a C# lock statement prevent race conditions when an API runs on multiple servers?","acceptedAnswer":{"@type":"Answer","text":"No, an in-process lock only coordinates threads sharing the same memory and does nothing across separate server instances. If two requests hit different servers, each acquires its own local lock and both can update the same database record simultaneously; database-level concurrency control, such as EF Core's rowversion token that throws DbUpdateConcurrencyException on conflicting writes, is needed instead. Teams scaling APIs across multiple instances can compare application locks versus database concurrency tokens on daily.dev."}}]}
```

