dotnet / dotnet/runtime

Should we consider using System.Threading.Lock instead of object if applicatable

Open
#132,808 4 comments 0 reactions 0 assignees View on GitHub
area-System.Threading question
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

.NET 9 introduced `System.Threading.Lock` specifically designed for synchronization, wondering that maybe we could use it instead of current `object`.

sample:

https://github.com/dotnet/runtime/blob/08df03ba6493589f3816e193847b50f105bf4c7a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.cs#L25

https://github.com/dotnet/runtime/blob/d1fcb014e484e7ab2d46b8144909a7a90c409da6/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.cs#L93

For easier build and clean code, maybe we could use a type alias to support for lower targets likes .NET standard 2.0 etc

configure implicit usings in MSbuild files:

```xml


```

or global usings in C#

```c#
#if NET9_0_OR_GREATER
using SyncLock = System.Threading.Lock;
#else
using SyncLock = System.Object;
#endif
```

Contributor guide

Open the contributing guide

Research direction

Start with the referenced Instrument.cs synchronization points and inspect the library's target frameworks. Compare the MSBuild type-alias approach with the conditional global using shown in the issue, then check the relevant build and test commands. Done means an agreed replacement strategy compiles for both .NET 9 and lower targets without changing synchronization behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, build-system
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.