An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# PowerThreadPool
![icon](https://raw.githubusercontent.com/ZjzMisaka/PowerThreadPool/main/icon.png)

![Nuget](https://img.shields.io/nuget/v/PowerThreadPool?style=for-the-badge)
![Nuget](https://img.shields.io/nuget/dt/PowerThreadPool?style=for-the-badge)
![GitHub release (with filter)](https://img.shields.io/github/v/release/ZjzMisaka/PowerThreadPool?style=for-the-badge)
![GitHub Repo stars](https://img.shields.io/github/stars/ZjzMisaka/PowerThreadPool?style=for-the-badge)
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/ZjzMisaka/PowerThreadPool/test.yml?style=for-the-badge)
[![Codecov](https://img.shields.io/codecov/c/github/ZjzMisaka/PowerThreadPool?style=for-the-badge)](https://app.codecov.io/gh/ZjzMisaka/PowerThreadPool)
[![CodeFactor](https://www.codefactor.io/repository/github/zjzmisaka/powerthreadpool/badge?style=for-the-badge)](https://www.codefactor.io/repository/github/zjzmisaka/powerthreadpool)

[![All Contributors](https://img.shields.io/badge/all_contributors-7-orange.svg?style=flat-square)](#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
ZjzMisaka

💻 🚧 📖
r00tee
r00tee

🤔
aadog
aadog

🐛
RookieZWH
RookieZWH

💬
hebinary
hebinary

💬
lindexi
lindexi

🐛

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