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.
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.
- Provides rich, detailed, and easy-to-use control primitives.
- Pool/Worker/Work have explicit states and are managed throughout their lifecycle, preventing orphan tasks.
- Offers native async support rather than merely wrapping
Task.Run, allowing PTP's advanced features to be applied to continuations. - Unifies the external interface for synchronous and asynchronous features.
- 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.
Access the Wiki in English | 中文 | 日本語.
Visit the DeepWiki for more information.
If you want to include PowerThreadPool in your project, you can install it directly from NuGet.
Support: Net40+ | Net5.0+ | netstandard2.0+
- Sync | Async
- Pool Control | Work Control
- Divide And Conquer
- Thread Pool Sizing
- Work Callback | Default Callback
- Rejection Policy
- Parallel Execution
- Work Priority | Thread Priority
- Error Handling
- Work Timeout | Cumulative Work Timeout
- Work Dependency
- Work Group
- Events
- Runtime Status
- Running Timer
- Queue Type (FIFO | LIFO | Deque | Custom)
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 ...;
});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
});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
});// 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. |
powershell -File build.ps1 -Version {Version}Testing And Performance Analysis | Feature Comparison | Knowledge Base
Get involved: Join our growing community
Thanks goes to these wonderful people (emoji key):
一条咸鱼 💻 |
ZjzMisaka 💻 🚧 📖 |
r00tee 🤔 |
aadog 🐛 |
RookieZWH 💬 |
hebinary 💬 |
lindexi 🐛 |
This project follows the all-contributors specification. Contributions of any kind welcome!