jason-c-daniels - Jcd.Threading 0.2.30
A netstandard2.0 library that provides utility classes to aid with thread creation, task scheduling and using synchronization primitives.
PM> Install-Package Jcd.Threading -Version 0.2.30 -Source https://www.myget.org/F/jason-c-daniels/api/v3/index.json
> nuget.exe install Jcd.Threading -Version 0.2.30 -Source https://www.myget.org/F/jason-c-daniels/api/v3/index.json
> dotnet add package Jcd.Threading --version 0.2.30 --source https://www.myget.org/F/jason-c-daniels/api/v3/index.json
source https://www.myget.org/F/jason-c-daniels/api/v3/index.json
nuget Jcd.Threading ~> 0.2.30
Copy to clipboard
> choco install Jcd.Threading --version 0.2.30 --source https://www.myget.org/F/jason-c-daniels/api/v2
Import-Module PowerShellGet
Register-PSRepository -Name "jason-c-daniels" -SourceLocation "https://www.myget.org/F/jason-c-daniels/api/v2"
Install-Module -Name "Jcd.Threading" -RequiredVersion "0.2.30" -Repository "jason-c-daniels"
Copy to clipboard
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
TaskSchedulerextensionRunto mimic theTask.RunAPI, ensuring tasks are run with the desired scheduler.IdleTaskScheduler, a custom task scheduler that schedules tasks in a round robin manner with idle threads.ThreadWrapperclass to simplify the process of making a pauseable thread.ItemProcessor<TItem>class encapsulating a queue+worker thread.- Various
Lockextension 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
Converted from Jcd.Tasks to Jcd.Threading
- .NETFramework 7.0: 7.0.0.0
- .NETStandard 2.0: 2.0.0.0
OwnersJason C. Daniels |
AuthorsJason C. Daniels |
Project URLhttps://github.com/jason-c-daniels/Jcd.Threading |
LicenseUnknown |
Info350 total downloads |
| 73 downloads for version 0.2.30 |
| Download (81.72 KB) |
| Found on the current feed only |
Package history
| Version | Size | Last updated | Downloads | Mirrored? | |||
|---|---|---|---|---|---|---|---|
|
|
0.2.30 | 81.72 KB | Sun, 10 Mar 2024 03:38:24 GMT | 73 |
|
||
|
|
0.2.26 | 81.75 KB | Sun, 10 Mar 2024 03:18:20 GMT | 86 |
|
||
|
|
0.2.22 | 90.42 KB | Sat, 09 Mar 2024 20:36:44 GMT | 61 |
|
||
|
|
0.2.13 | 54.14 KB | Thu, 07 Mar 2024 04:53:24 GMT | 63 |
|
||
|
|
0.2.12 | 54.16 KB | Thu, 07 Mar 2024 04:24:56 GMT | 67 |
|