Buffering 0.4.0
dotnet add package Buffering --version 0.4.0
NuGet\Install-Package Buffering -Version 0.4.0
<PackageReference Include="Buffering" Version="0.4.0" />
paket add Buffering --version 0.4.0
#r "nuget: Buffering, 0.4.0"
// 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 | Versions 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. |
-
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.
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.