Backport.System.Threading.Lock
2.0.0
See the version list below for details.
dotnet add package Backport.System.Threading.Lock --version 2.0.0
NuGet\Install-Package Backport.System.Threading.Lock -Version 2.0.0
<PackageReference Include="Backport.System.Threading.Lock" Version="2.0.0" />
<PackageVersion Include="Backport.System.Threading.Lock" Version="2.0.0" />
<PackageReference Include="Backport.System.Threading.Lock" />
paket add Backport.System.Threading.Lock --version 2.0.0
#r "nuget: Backport.System.Threading.Lock, 2.0.0"
#:package Backport.System.Threading.Lock@2.0.0
#addin nuget:?package=Backport.System.Threading.Lock&version=2.0.0
#tool nuget:?package=Backport.System.Threading.Lock&version=2.0.0
Backport.System.Threading.Lock
A micro-library that backports .NET 9.0+'s System.Threading.Lock to prior framework versions (from .NET Framework 3.5 up to .NET 8.0), providing as much backward compatibility as possible.
Usage (only if targeting frameworks prior to .NET 5.0)
Due to frameworks prior to .NET 5.0 supporting the notorious Thread.Abort, we cannot use the same System.Threading.Lock namespace or else the locks would not be hardened against thread aborts, so we need to use a creator method instead.
Remember to multi-target .NET 9.0 in your .csproj file as well in order to get the performance benefits of System.Threading.Lock.
Example:
<TargetFrameworks>netstandard2.0;net9.0</TargetFrameworks>
In your .csproj file, or ideally in your Directory.Build.props file to avoid doing it to all projects, do the following:
<ItemGroup>
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
</ItemGroup>
Usage:
private readonly Lock _syncRoot = LockFactory.Create();
public void Foo()
{
lock (_syncRoot)
{
// do something
}
}
public void Bar()
{
_syncRoot.Enter();
// do something that cannot crash on a thread that cannot abort
_syncRoot.Exit();
}
Use the Lock class the same way you would use System.Threading.Lock.
Usage (only if targeting .NET 5.0 or later)
Use this library the same way you would use System.Threading.Lock.
In order to get the performance benefits of System.Threading.Lock, you must however multi-target frameworks in your .csproj file.
Example:
<TargetFrameworks>net5.0;net9.0</TargetFrameworks>
There is also no need to reference this library as a dependency for .NET 9.0+. You can achieve that by having this in your .csproj file:
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<PackageReference Include="Backport.System.Threading.Lock" Version="2.0.0" />
</ItemGroup>
Performance
This library was benchmarked against locking on an object on .NET 8.0 and no speed or memory allocation difference was noted, whereas when .NET 9.0 was used the performance was ~25% better as opposed to locking on an object.
Credits
Check out our list of contributors!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 3.5
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETFramework 4.5
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (11)
Showing the top 5 NuGet packages that depend on Backport.System.Threading.Lock:
| Package | Downloads |
|---|---|
|
DisCatSharp.VoiceNext
DisCatSharp Voice Next Extension Easy made audio player for discord bots. Documentation: https://docs.dcs.aitsys.dev/articles/modules/audio/voicenext/prerequisites.html |
|
|
CarinaStudio.AppBase.Core
AppBase is a base library set for .NET application. CarinaStudio.AppBase.Core is the core library of AppBase, provides extensions for object, collection and threading to make your code more elegant and clear. |
|
|
EasyMemoryCache
MemoryCache component, for easy MemoryCache implementation |
|
|
NetCorePal.Extensions.Snowflake
NetCorePal Cloud Framework |
|
|
Juice
Juice core - Media Managment System. |
GitHub repositories (4)
Showing the top 4 popular GitHub repositories that depend on Backport.System.Threading.Lock:
| Repository | Stars |
|---|---|
|
MarimerLLC/csla
A home for your business logic in any .NET application.
|
|
|
Listenarrs/Listenarr
Listenarr automates audiobook collection management similar to Sonarr or Radarr, but for audiobooks. It can search, download, and organize your library automatically using metadata from Audible and other sources.
|
|
|
netcorepal/netcorepal-cloud-framework
一个基于ASP.NET Core实现的整洁领域驱动设计落地战术框架。 A tactical framework for Clean Domain-Driven Design based on ASP.NET Core.
|
|
|
nauful/LibUA
Open-source OPC UA client and server library
|
Breaking change that requires different usage for projects targeting frameworks prior to .NET 5.0 in order to cater for potential thread aborts.