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, detailed, and easy-to-use control primitives.
  2. Pool/Worker/Work have explicit states and are managed throughout their lifecycle, preventing orphan tasks.
  3. Offers native async support rather than merely wrapping Task.Run, allowing PTP's advanced features to be applied to continuations.
  4. Unifies the external interface for synchronous and asynchronous features.
  5. Leverages optimizations like CAS, work stealing, and heuristic debounce algorithms to balance functionality and performance.

PTP is suitable for scenarios with complex synchronous/asynchronous work control and management needs, such as batch/data processing pipeline tools, game engine extension tools, and desktop applications relying on user interaction.
However, if your product runs on very low performance devices, consists mainly of transient "fire and forget" works, requires optimizing every nanosecond, or if you and your team already have a comfortable workflow and framework, there is no need to introduce it.

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.
? A CancellationToken. When the Stop(...) function is called, the notification will be correctly propagated to this CancellationToken.

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