Multipool
Multipool is a Rust library that provides a configurable and efficient thread pool with optional work-stealing capabilities. It allows you to easily spawn tasks and handle concurrent workloads, while also offering a builder API to customize the number of threads and choose between the global queue or the work-stealing mode.
Features
- Configurable Threads: Specify the number of worker threads for concurrent task handling.
- Global Queue or Work-Stealing: Choose between simple global queues or efficient work-stealing for load balancing.
- Priority Scheduling: Assign priorities to tasks for more control over execution order.
- Metrics and Monitoring: Track active threads, queued tasks, running tasks and completed tasks in real-time.
Installation
Add multipool
as a dependency in your Cargo.toml
:
[]
= "0.2"
Examples
Basic Usage with Work-Stealing
use ThreadPoolBuilder;
Using a Global Queue
use ThreadPoolBuilder;
Using Priority Scheduling
With version 0.2.0
, Multipool supports priority scheduling. You can assign priorities to tasks when using the priority
mode.
use ThreadPoolBuilder;
In this example:
- Tasks with higher priority (low
i
values) will execute before lower-priority tasks. - The
spawn_with_priority
method allows you to specify the priority level for each task.
Using TaskHandle
to Retrieve Results
In some cases, you might want to retrieve the result of a task after it completes. This can be done using the TaskHandle
returned by the spawn
or spawn_with_priority
method.
In this example:
- The
spawn
method returns aTaskHandle
. - The
join
method blocks until the task completes and retrieves the result. - Ensure to handle potential errors from
join
gracefully, such as usingunwrap
or other error-handling mechanisms.
Live Metrics Monitoring
With version 0.2.3
, Multipool introduces live metrics monitoring to track the thread pool's performance in real-time. You can monitor the following metrics:
- Queued Tasks: The number of tasks waiting to be executed.
- Running Tasks: The number of tasks currently being executed.
- Completed Tasks: The number of tasks that have finished execution.
- Active Threads: The number of threads currently active in the thread pool.
Example:
use ;
use Arc;
use Duration;
use thread;
In this example:
- Metrics are updated in real-time by the thread pool.
- A monitoring thread periodically logs the metrics to the terminal.
- The metrics are accessible through the
ThreadPoolMetrics
struct, which uses atomic counters for thread-safe updates.
Documentation
Feedback
If you encounter any issues or have feature requests, please open an issue in the GitHub repository issues.