Open.Threading.ReadWrite 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Open.Threading.ReadWrite --version 3.0.0                
NuGet\Install-Package Open.Threading.ReadWrite -Version 3.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Open.Threading.ReadWrite" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Open.Threading.ReadWrite --version 3.0.0                
#r "nuget: Open.Threading.ReadWrite, 3.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Open.Threading.ReadWrite as a Cake Addin
#addin nuget:?package=Open.Threading.ReadWrite&version=3.0.0

// Install Open.Threading.ReadWrite as a Cake Tool
#tool nuget:?package=Open.Threading.ReadWrite&version=3.0.0                

Open.Threading.ReadWrite

Useful set of extensions and classes for simplifying and optimizing read-write synchronization with ReaderWriterLockSlim.

GitHub license 100% code coverage NuGet

Purpose

There are very common but tedious patterns to follow when utilizing a ReaderWriterLockSlim. Some of the more complex but useful patterns involve reading (with or without a read lock) and then if a condition is met, acquiring a write lock before proceeding.

This extension library removes the tediousness of properly acquiring and releasing various locks and provides easy access to timeout values if needed.

Basics


Read

rwLockSlim.Read(() =>
{
    /* do work inside a read lock */
});
using(rwLockSlim.ReadLock())
{
    /* do work inside a read lock */
}
int result = rwLockSlim.Read(() =>
{
    int i;
    /* produce a result inside read lock */
    return i;
});

Write

rwLockSlim.Write(() =>
{
    /* do work inside a read lock */
});
using(rwLockSlim.WriteLock())
{
    /* do work inside a write lock */
}
int result = rwLockSlim.Write(() =>
{
    int i;
    /* produce a result inside write lock */
    return i;
});

Upgradable Read

using(rwLockSlim.UpgradableReadLock())
{
    /* do work inside an upgradable read lock */
    if(condition)
    {
        using(rwLockSlim.WriteLock())
        {
            /* upgraded to a write lock */
        }
    }
}

With Timeouts

These throw a TimeoutException if a lock cannot be acquired within the time specified.


Read

rwLockSlim.Read(1000 /* ms */, () =>
{
    /* do work inside a read lock */
});

or

using(rwLockSlim.ReadLock(1000)) // ms
{
    /* do work inside a read lock */
}

Write

rwLockSlim.Write(1000 /* ms */, () =>
{
    /* do work inside a write lock */
});

or

using(rwLockSlim.WriteLock(1000)) // ms
{
    /* do work inside a write lock */
}

Advanced Examples


WriteConditional

This example demonstrates how to properly query a value before writing with a 1 second timeout.

var actionWasInvoked = rwLockSlim.WriteConditional(1000 /* ms */,
() => /* condition that is queried inside an upgradable read lock */,
() => /* do work inside a write lock */);

ReadWriteConditional

This more advanced example optimizes the process of reading and then writing by first testing the condition within a read lock before attempting with an upgradable read lock.

int result = 0;
bool actionWasInvoked = rwLockSlim.ReadWriteConditional(ref result,
isUpgraded => /* condition that is first queried inside a read lock */,
() =>
{
    int i;
    /* do work inside a write lock */
    return i;
});
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.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 is compatible. 
.NET Framework 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Open.Threading.ReadWrite:

Package Downloads
Open.Threading.ReadWriteHelper

A utility for read-write synchronizing by context. Useful for synchronizing collections that are not inherently thread-safe. Reference to Open.Threading.ReadWrite exposes extensions for ReaderWriterLockSlim. Part of the "Open" set of libraries.

ObservableValue

A class representing a value that when updated (posted) signals changes to observers. Will post current value (if initialized) when subscribing. Synchronized to ensure ordering. Compatible with IObservable<T> and System.Reactive.

LiveStreamingServerNet.StreamProcessor

This package provides the capability to transmux RTMP streams into HLS format, and integrates with FFmpeg for stream processing.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Open.Threading.ReadWrite:

Repository Stars
josephnhtam/live-streaming-server-net
A .NET implementation of RTMP live streaming server, supporting HTTP-FLV, WebSocket-FLV, HLS, Kubernetes, cloud storage services integration and more.
Version Downloads Last updated
3.0.1 625 11/14/2024
3.0.0 103 11/14/2024
2.0.3 19,322 3/24/2022
2.0.2 40,484 3/22/2022
2.0.1 1,451 3/22/2022
2.0.0 686 3/22/2022
2.0.0-preview3 880 3/21/2022
2.0.0-preview2 942 3/21/2022
2.0.0-preview1 426 3/19/2022
1.4.8 4,156 9/29/2021
1.4.5 1,584 7/11/2021
1.4.4 895 7/11/2021
1.4.3 1,773 7/7/2021
1.4.0 53,084 7/17/2020
1.3.3 16,671 1/4/2020
1.3.2 20,038 7/4/2019
1.1.2 7,959 7/18/2018