https://github.com/zjzmisaka/powerthreadpool
A comprehensive and efficient lock-free thread pool with granular work control, flexible concurrency, and robust error handling, alongside an easy-to-use API for diverse work submissions.
https://github.com/zjzmisaka/powerthreadpool
async coroutine csharp dotnet lock-free lockfree task thread thread-pool threadpool
Last synced: about 2 months ago
JSON representation
A comprehensive and efficient lock-free thread pool with granular work control, flexible concurrency, and robust error handling, alongside an easy-to-use API for diverse work submissions.
- Host: GitHub
- URL: https://github.com/zjzmisaka/powerthreadpool
- Owner: ZjzMisaka
- License: mit
- Created: 2023-08-25T07:07:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-16T08:54:31.000Z (9 months ago)
- Last Synced: 2025-05-16T14:04:54.677Z (9 months ago)
- Topics: async, coroutine, csharp, dotnet, lock-free, lockfree, task, thread, thread-pool, threadpool
- Language: C#
- Homepage: https://www.nuget.org/packages/PowerThreadPool/
- Size: 1.08 MB
- Stars: 139
- Watchers: 3
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# PowerThreadPool






[](https://app.codecov.io/gh/ZjzMisaka/PowerThreadPool)
[](https://www.codefactor.io/repository/github/zjzmisaka/powerthreadpool)
[](#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.
## Documentation
Access the Wiki in [English](https://github.com/ZjzMisaka/PowerThreadPool/wiki) | [中文](https://github.com/ZjzMisaka/PowerThreadPool.zh-CN.Wiki/wiki) | [日本語](https://github.com/ZjzMisaka/PowerThreadPool.ja-JP.Wiki/wiki).
Visit the [DeepWiki](https://deepwiki.com/ZjzMisaka/PowerThreadPool) for more information.
## Pack
```ps1
powershell -File build.ps1 -Version {Version}
```
## Installation
If you want to include PowerThreadPool in your project, you can [install it directly from NuGet](https://www.nuget.org/packages/PowerThreadPool/).
Support: Net40+ | Net5.0+ | netstandard2.0+
## Features
- [Sync | Async](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Sync-Async)
- [Pool Control | Work Control](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control)
- [Stop](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control#pause-resume-stop)
- [Pause](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control#pause-resume-stop)
- [Resume](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control#pause-resume-stop)
- [Force Stop](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control#force-stop)
- [Wait](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control#wait)
- [Fetch](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control#fetch)
- [Cancel](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Control#cancel)
- [Divide And Conquer](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Divide-And-Conquer)
- [Thread Pool Sizing](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Thread-Pool-Sizing)
- [Idle Thread Scheduled Destruction](https://github.com/ZjzMisaka/PowerThreadPool/wiki/DestroyThreadOption)
- [Thread Starvation Countermeasures (Long-running Work Support)](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Thread-Pool-Sizing#thread-starvation)
- [Work Callback | Default Callback](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Callback)
- [Rejection Policy](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Rejection-Policy)
- [Parallel Execution](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Parallel-Execution)
- [For](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Parallel-Execution#For)
- [ForEach](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Parallel-Execution#ForEach)
- [Watch](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Parallel-Execution#Watch)
- [Work Priority | Thread Priority](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Priority)
- [Error Handling](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Error-Handling)
- [Retry](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Retry)
- [Work Timeout | Cumulative Work Timeout](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Timeout)
- [Work Dependency](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Dependency)
- [Work Group](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Group)
- [Group Control](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Work-Group#group-control)
- [Group Relation](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Group-Relation)
- [Events](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Events)
- [Runtime Status](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Runtime-Status)
- [Running Timer](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Running-Timer)
- [Queue Type (FIFO | LIFO | Deque | Custom)](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Queue-Type)
- [Load Balancing](https://en.wikipedia.org/wiki/Work_stealing)
- [Low-Contention Design](https://en.wikipedia.org/wiki/Non-blocking_algorithm)
## 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.
```csharp
PowerPool powerPool = new PowerPool();
// Sync
powerPool.QueueWorkItem(() =>
{
// Do something
});
// Async
powerPool.QueueWorkItemAsync(async () =>
{
// Do something
// await ...;
});
```
### With callback
```csharp
PowerPool powerPool = new PowerPool(new PowerPoolOption() { /* Some options */ });
// Sync
powerPool.QueueWorkItem(() =>
{
// Do something
return result;
}, (res) =>
{
// Callback of the work
});
// Async
powerPool.QueueWorkItemAsync(async () =>
{
// Do something
// await ...;
}, (res) =>
{
// Callback of the work
});
```
### With option
```csharp
PowerPool powerPool = new PowerPool(new PowerPoolOption() { /* Some options */ });
// Sync
powerPool.QueueWorkItem(() =>
{
// Do something
return result;
}, new WorkOption()
{
// Some options
});
// Async
powerPool.QueueWorkItemAsync(async () =>
{
// Do something
// await ...;
}, new WorkOption()
{
// Some options
});
```
### Reference
``` csharp
WorkID QueueWorkItem(Action action, T1 param1, ..., *);
WorkID QueueWorkItem(Action action, *);
WorkID QueueWorkItem(Action action, object[] param, *);
WorkID QueueWorkItem(Func function, T1 param1, ..., *);
WorkID QueueWorkItem(Func function, *);
WorkID QueueWorkItem(Func function, object[] param, *);
WorkID QueueWorkItemAsync(Func asyncFunc, *);
WorkID QueueWorkItemAsync(Func> asyncFunc, *);
WorkID QueueWorkItemAsync(Func asyncFunc, out Task task, *);
WorkID QueueWorkItemAsync(Func> asyncFunc, out Task> task, *);
```
#### Asterisk `*`
1. `WorkOption` | `WorkOption`: The work option to customize the behavior of the work.
2. `Action>`: The callback to be invoked when the work is completed.
#### Ellipses `...`
1. Up to 5 type parameters are supported.
## More
[Testing And Performance Analysis](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Testing-And-Performance-Analysis) | [Feature Comparison](https://github.com/ZjzMisaka/PowerThreadPool/wiki/Feature-Comparison)
**Get involved**: [Join our growing community](https://github.com/ZjzMisaka/PowerThreadPool/discussions/258)
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

一条咸鱼
💻

ZjzMisaka
💻 🚧 📖

r00tee
🤔

aadog
🐛

RookieZWH
💬

hebinary
💬

lindexi
🐛
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!