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.
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>netstandard2.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="1.1.6" />
</ItemGroup>
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.
Check out our list of contributors!