Open.Threading.ReadWrite 2.0.3

.NET Standard 2.0
NuGet\Install-Package Open.Threading.ReadWrite -Version 2.0.3
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.
dotnet add package Open.Threading.ReadWrite --version 2.0.3
<PackageReference Include="Open.Threading.ReadWrite" Version="2.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Open.Threading.ReadWrite --version 2.0.3
#r "nuget: Open.Threading.ReadWrite, 2.0.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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=2.0.3

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

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 Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (2)

Showing the top 2 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.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.3 108 3/24/2022
2.0.2 2,535 3/22/2022
2.0.1 84 3/22/2022
2.0.0 63 3/22/2022
2.0.0-preview3 48 3/21/2022
2.0.0-preview2 53 3/21/2022
2.0.0-preview1 60 3/19/2022
1.4.8 668 9/29/2021
1.4.5 618 7/11/2021
1.4.4 278 7/11/2021
1.4.3 897 7/7/2021
1.4.0 37,788 7/17/2020
1.3.3 13,353 1/4/2020
1.3.2 17,433 7/4/2019
1.1.2 5,718 7/18/2018