A micro-library that backports/polyfills .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. Optionally works as a source generator.
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.
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.
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>
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!