Buffering 0.3.2
See the version list below for details.
dotnet add package Buffering --version 0.3.2
NuGet\Install-Package Buffering -Version 0.3.2
<PackageReference Include="Buffering" Version="0.3.2" />
paket add Buffering --version 0.3.2
#r "nuget: Buffering, 0.3.2"
// 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 | 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. |
-
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.
Added parallelization delegate for working over entire data chunk instead of by individual data