Buffering 0.4.0

dotnet add package Buffering --version 0.4.0                
NuGet\Install-Package Buffering -Version 0.4.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="Buffering" Version="0.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Buffering --version 0.4.0                
#r "nuget: Buffering, 0.4.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 Buffering as a Cake Addin
#addin nuget:?package=Buffering&version=0.4.0

// Install Buffering as a Cake Tool
#tool nuget:?package=Buffering&version=0.4.0                

Buffering

This library provides very easy and streamlined functionality for implementing different kinds of buffers in any system. A double buffer that is configurable for every step of the way is provided in this library.

Threadsafe

All buffers are implemented in a threadsafe manner using the most efficient object-oriented implementation involving a lock and handle that only needs to be disposed after you're done. However, there is also a no-cost NoLock implementation that can be used as well if synchronization isn't a concern.

Double Buffer Example

Here is an example of how simple it is to set up a double buffer. This creates a double buffer and a cancellation token source to define a runtime length of the program:

var db = new DoubleBuffer<object>(
    new SystemThreadingLock(),
    DoubleBufferSwapEffect.Flip);

var cts = new CancellationTokenSource(10_000);

This next part creates the long-running back buffer update thread where the back buffer is updated:

var bufferUpdateTask = new TaskFactory(TaskCreationOptions.LongRunning, 0).StartNew(() =>
{
    var token = cts.Token;
    var writer = db.BackWriter;
    while (!token.IsCancellationRequested)
    {
        var obj = GetResult();
        writer.UpdateBackBuffer(obj);
        writer.SwapBuffers();
    }
});

Finally, the portion of the program that reads the buffer until the task is over and it updates no more:

var reader = db.FrontReader;
while (!bufferUpdateTask.IsCompleted)
{
    reader.ReadFrontBuffer(out var rsc, out var rscInfo).Dispose();
    Console.WriteLine($"{rscInfo.Id:N0}: {rsc} : {rscInfo}");
}

That's all there is to creating a double buffer and using it at very performant speeds with minimal or no lock times.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.0 60 11/26/2024
0.3.2 187 12/21/2023
0.3.1 109 12/18/2023
0.3.0 94 12/17/2023
0.2.0 131 12/1/2023
0.1.1 120 11/19/2023
0.1.0 110 11/17/2023

Heavily refactored the library to make usage as simple as possible and removed copy swap effect. Previously, a no-cost implementation allowed the double buffer to use all swap effects. However, flipping is much faster than copying, and copying isn't necessary as the user can track the variable the back buffer is pulling from. Removing copying also allowed serious simplification.