https://github.com/ntdls/ntdls.delegatethreadpooling
High performance active thread pool where work items can be queued as delegate functions. Allows you to easily enqueue infinite FIFO worker items or enforce queue size, wait on collections of those items to complete, and total control over the pool size. Also allows for multiple pools, so that different workloads do not interfere with one another.
https://github.com/ntdls/ntdls.delegatethreadpooling
fifo fifo-queue high-performance queue threading threadpool threadpooling
Last synced: 22 days ago
JSON representation
High performance active thread pool where work items can be queued as delegate functions. Allows you to easily enqueue infinite FIFO worker items or enforce queue size, wait on collections of those items to complete, and total control over the pool size. Also allows for multiple pools, so that different workloads do not interfere with one another.
- Host: GitHub
- URL: https://github.com/ntdls/ntdls.delegatethreadpooling
- Owner: NTDLS
- License: mit
- Created: 2024-01-17T16:24:00.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-22T15:18:05.000Z (7 months ago)
- Last Synced: 2024-11-14T09:08:29.597Z (6 months ago)
- Topics: fifo, fifo-queue, high-performance, queue, threading, threadpool, threadpooling
- Language: C#
- Homepage: https://networkdls.com/
- Size: 389 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NTDLS.DelegateThreadPooling
📦 Be sure to check out the NuGet package: https://www.nuget.org/packages/NTDLS.DelegateThreadPooling
High performance active thread pool where work items can be queued as delegate functions.
Allows you to easily enqueue infinite FIFO worker items or enforce queue size, wait on collections
of those items to complete, and total control over the pool size. Also allows for multiple pools,
so that different workloads do not interfere with one another._If you have ever been frustrated with **System.Threading.ThreadPool**, then this is likely the solution you are looking for._
```cs
private static readonly DelegateThreadPool _delegateThreadPool = new(10);static void Main()
{
CollectionExample();
NoCollectionExample();Console.WriteLine("Press [enter] to exit.");
Console.ReadLine();_delegateThreadPool.Dispose();
}private static void CollectionExample()
{
Console.WriteLine("CollectionExample: Starting to enqueue items...");var childPool = _delegateThreadPool.CreateChildPool();
//Enqueue work items as delegate functions.
for (int i = 0; i < 100; i++)
{
childPool.Enqueue(() =>
{
Thread.Sleep(1000); //Do some work...
});
}Console.WriteLine("Enqueue complete, waiting on completion.");
//Wait on all of the workitems to complete.
childPool.WaitForCompletion();Console.WriteLine("All workers are complete.");
}private static void NoCollectionExample()
{
Console.WriteLine("NoCollectionExample: Starting to enqueue items...");var itemStates = new List>();
//Enqueue work items as delegate functions.
for (int i = 0; i < 100; i++)
{
var queueItemState = _delegateThreadPool.Enqueue(() =>
{
Thread.Sleep(1000); //Do some work...
});itemStates.Add(queueItemState);
}Console.WriteLine("Enqueue complete, waiting on completion.");
//Wait on all of the workitems to complete.
itemStates.ForEach(t => t.WaitForCompletion());Console.WriteLine("All workers are complete.");
}
```