Skip to content

ZjzMisaka/PowerThreadPool

Repository files navigation

PowerThreadPool

icon

.NET Foundation Seed Project

Nuget Nuget GitHub release (with filter) GitHub Repo stars GitHub Workflow Status (with event) Codecov CodeFactor All Contributors

A comprehensive and efficient low-contention thread pool for easily managing both sync and async workloads. It provides granular work control, flexible concurrency, and robust error handling.

Status

This project is currently a Seed Project within the .NET Foundation. This means it is an "up and coming" project within the foundation to grow its community and ecosystem.

Architecture

image

Why PTP

  1. Provides rich, fine-grained control primitives spanning the entire work lifecycle.
  2. Offers native async support. PTP manages all continuations of an asynchronous work directly without sacrificing async semantics and essential characteristics, rather than simply wrapping Task.Run.
  3. Grants asynchronous works the exact same control features as synchronous ones via a unified interface, allowing transparent and seamless interleaving of synchronous and asynchronous workloads.
  4. Leverages optimizations like CAS, work-stealing, and heuristic debounce algorithms. This maintains performance close to the native thread pool while implementing advanced functionality, thereby minimizing the overhead caused by secondary encapsulation.

Documentation

Access the Wiki in English | 中文 | 日本語.
Visit the DeepWiki for more information.

Installation

If you want to include PowerThreadPool in your project, you can install it directly from NuGet.
Support: Net40+ | Net5.0+ | netstandard2.0+

Features

Internal Design

Getting Started

Out-of-the-box: Run a simple work

PTP is designed to be out-of-the-box. For simple works, you can get started without any complex configuration.

PowerPool powerPool = new PowerPool();
// Sync
powerPool.QueueWorkItem(() => 
{
    // Do something
});
// Async
powerPool.QueueWorkItem(async () =>
{
    // Do something
    // await ...;
});

With callback

PowerPool powerPool = new PowerPool(new PowerPoolOption() { /* Some options */ });
// Sync
powerPool.QueueWorkItem(() => 
{
    // Do something
    return result;
}, (res) => 
{
    // Callback of the work
});
// Async
powerPool.QueueWorkItem(async () =>
{
    // Do something
    // await ...;
}, (res) =>
{
    // Callback of the work
});

With option

PowerPool powerPool = new PowerPool(new PowerPoolOption() { /* Some options */ });
// Sync
powerPool.QueueWorkItem(() => 
{
    // Do something
    return result;
}, new WorkOption()
{
    // Some options
});
// Async
powerPool.QueueWorkItem(async () =>
{
    // Do something
    // await ...;
}, new WorkOption()
{
    // Some options
});

Reference

// Sync
WorkID QueueWorkItem<T1, ...>(Action<T1, ...> action, T1 param1, ..., *);
WorkID QueueWorkItem(Action action, *);
WorkID QueueWorkItem(Action<object[]> action, object[] param, *);
WorkID QueueWorkItem<T1, ..., TResult>(Func<T1, ..., TResult> function, T1 param1, ..., *);
WorkID QueueWorkItem<TResult>(Func<TResult> function, *);
WorkID QueueWorkItem<TResult>(Func<object[], TResult> function, object[] param, *);
// Async
WorkID QueueWorkItem<T1, ...>(Func<T1, ..., Task> asyncFunc, T1 param1, ..., _, *);
WorkID QueueWorkItem(Func<Task> asyncFunc, _, *);
WorkID QueueWorkItem(Func<object[], Task> asyncFunc, object[] param, _, *);
WorkID QueueWorkItem<T1, ..., TResult>(Func<T1, ..., Task<TResult>> asyncFunc, T1 param1, ..., _, *);
WorkID QueueWorkItem<TResult>(Func<Task<TResult>> asyncFunc, _, *);
WorkID QueueWorkItem<TResult>(Func<object[], Task<TResult>> asyncFunc, object[] param, _, *);
Wildcard Explanation
* WorkOption | WorkOption<T>: The work option to customize the behavior of the work.
Action<ExecuteResult<T>>: The callback to be invoked when the work is completed.
... Up to 5 type parameters are supported.
_ An out parameter (Task | Task<ExecuteResult<TResult>>) that returns a Task representing the asynchronous execution of asyncFunc.

Pack

powershell -File build.ps1 -Version {Version}

More Resources

Testing And Performance Analysis | Feature Comparison | Knowledge Base
Get involved: Join our growing community

Contributors ✨

Thanks goes to these wonderful people (emoji key):

一条咸鱼
一条咸鱼

💻
ZjzMisaka
ZjzMisaka

💻 🚧 📖
r00tee
r00tee

🤔
aadog
aadog

🐛
RookieZWH
RookieZWH

💬
hebinary
hebinary

💬
lindexi
lindexi

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

About

A comprehensive and efficient low-contention thread pool for easily managing both sync and async workloads. It provides granular work control, flexible concurrency, and robust error handling.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Contributors

Languages