Buffering 0.3.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Buffering --version 0.3.2                
NuGet\Install-Package Buffering -Version 0.3.2                
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.3.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Buffering --version 0.3.2                
#r "nuget: Buffering, 0.3.2"                
#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.3.2

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

Buffering

This library provides very easy and streamlined functionality for implementing different kinds of buffers in any system. Using the package, you will have a single buffer and double buffer for wherever you need these in your code. There are also methods for easily partitioning data and optionally processing the partitioned data in parallel without relying on a task scheduler and unnecessary synchronization in most cases from the BCL-provided System.Threading.Tasks.Parallel class.

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.

Double Buffer Example

Here is an example of how simple it is to set up a double buffer. A single buffer is very similar but uses the respectively named classes:

This creates a double buffer using the given configuration (yeah, multiple ways you can define how a buffer works) and a cancellation token source to define a runtime length of the program:

var db = new DoubleBuffer<Vector3>(
    rsc: new BufferingResource<Vector3>(
        init: () => Vector3.Zero,
        updater: (ref Vector3 rsc, bool _) => rsc = new Vector3(rsc.X + 1F)),
    configuration: new DoubleBufferConfiguration(
        lockImpl: new MonitorLock(),
        swapEffect: DoubleBufferSwapEffect.Flip));

var cts = new CancellationTokenSource(10_000);

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

var bufferUpdateTask = new TaskFactory(TaskCreationOptions.LongRunning, 0).StartNew(() =>
{
    var token = cts.Token;
    var controller = db.BackController;
    while (!token.IsCancellationRequested)
    {
        controller.UpdateBackBuffer();
        controller.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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.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 45 11/26/2024
0.3.2 183 12/21/2023
0.3.1 105 12/18/2023
0.3.0 88 12/17/2023
0.2.0 125 12/1/2023
0.1.1 116 11/19/2023
0.1.0 105 11/17/2023

Added parallelization delegate for working over entire data chunk instead of by individual data