Jcd.Threading 0.2.30

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Jcd.Threading --version 0.2.30
NuGet\Install-Package Jcd.Threading -Version 0.2.30
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="Jcd.Threading" Version="0.2.30" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Jcd.Threading --version 0.2.30
#r "nuget: Jcd.Threading, 0.2.30"
#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 Jcd.Threading as a Cake Addin
#addin nuget:?package=Jcd.Threading&version=0.2.30

// Install Jcd.Threading as a Cake Tool
#tool nuget:?package=Jcd.Threading&version=0.2.30

Jcd.Threading

A netstandard2.0 library that provides utility classes to help simplify some aspects of multi-threading and synchronization.

Read the API documentation carefully.

Features

  • TaskScheduler extension Run to mimic the Task.Run API, ensuring tasks are run with the desired scheduler.
  • IdleTaskScheduler, a custom task scheduler that schedules tasks in a round robin manner with idle threads.
  • ThreadWrapper class to simplify the process of making a pauseable thread.
  • ItemProcessor<TItem> class encapsulating a queue+worker thread.
  • Various Lock extension methods to simplify using synchronization primitives ( e.g. SemaphoreSlimExtensions)
  • Various synchronized value holder generics to automatically use and release a specific locking mechanism when getting or setting a value. (e.g. ReaderWriterLockSlimValue)

Examples

Execute TaskSchedulerExtensions.Run on a default instance of IdleTaskScheduler configured to provide 4 STA threads.

using Jcd.Threading;
using Jcd.Threading.Tasks;

var its = new IdleTaskScheduler(4,ApartmentState.STA,"My Scheduler");
its.Run(()=>{/* do some work.*/});

Use SemaphoreSlimExtensions.Lock to automatically call Wait and Release

using Jcd.Threading;

var sem = new SemaphoreSlim(1,1);
using (sem.Lock()) // This calls sem.Wait
{
   // This is your critical section. 
   // Do only what needs to be done
   // while the lock is held. Do everything 
   // else before or after.
}
// once .Dispose is called, sem.Release() is called.

Use ReaderWriterLockSlimValue to synchronize reads and writes to a single shared value. Useful for needing exclusive write access for multiple readers whose use case can tolerate slightly stale data, such as thread specific status updates.

using Jcd.Threading.SynchronizedValues;

const int initialValue=20;
var rwlsv = new ReaderWriterLockSlimValue<int>(initialValue);

// writer thread.
var wt = Task.Run(()=>{
   var sw = StopWatch.StartNew();
   int i=0;
   do
   {
      i++;
      rwlsv.Value = i;   // here we communicate thread-local information blocking all reads as its written.
      Thread.Sleep(100); // wait 0.1 seconds 
   }
   while(sw.ElapsedMilliseconds < 1000)
});

// reader thread 1.
var rt1 = Task.Run(()=>{
   var sw=StopWatch.StartNew();
   do
   {
      // here we read the data maintained by the writer thread, blocking writes as its read.
      Console.WriteLine($"[1] The count is: {rwlsv.Value}");
      Thread.Sleep(71); 
   }
   while(sw.ElapsedMilliseconds < 750)
});

// reader thread 2.
var rt1 = Task.Run(()=>{
   var sw=StopWatch.StartNew();
   do
   {
      // here we read the data maintained by the writer thread, blocking writes as its read.
      Console.WriteLine($"[2] The count is: {rwlsv.Value}");
      Thread.Sleep(50); 
   }
   while(sw.ElapsedMilliseconds < 1500)
});

// wait for all threads to finish.
await Task.WhenAll(new[]{wt,rt1,rt2});

These were just an overview of what's available. See EXAMPLES.md for more and detailed examples.

And as always, read the API documentation

Badges

GitHub Build status CodeFactor Grade Quality Gate Status

MyGet Nuget

API Docs

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

    • No dependencies.
  • net7.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.2.30 89 3/10/2024

Converted from Jcd.Tasks to Jcd.Threading