Crate multipool

Source
Expand description

§multipool

multipool is a Rust library that provides a configurable thread pool with both a standard task queue and work-stealing capabilities.

§Features

  • Spawn tasks into a shared queue.
  • Optional work-stealing mode for improved concurrency and load balancing.
  • Graceful shutdown.
  • Priority-based task scheduling.
  • Configurable number of threads.
  • Metrics collection for monitoring thread pool activity.

§Usage

§Basic Usage

use multipool::ThreadPoolBuilder;

// Create a thread pool with default settings (4 threads)
let pool = ThreadPoolBuilder::new().build();

// Spawn a task and retrieve its result
let handle = pool.spawn(|| {
    println!("Hello from the thread pool!");
});
handle.join().unwrap();

// Shut down the pool
pool.shutdown();

§Changing the Number of Threads

use multipool::ThreadPoolBuilder;

// Create a thread pool with 8 threads
let pool = ThreadPoolBuilder::new().num_threads(8).build();

// Spawn tasks
for i in 0..8 {
    pool.spawn(move || {
        println!("Task {} executed", i);
    });
}

pool.shutdown();

§Work-Stealing Mode

use multipool::ThreadPoolBuilder;

// Create a thread pool with work-stealing enabled
let pool = ThreadPoolBuilder::new()
    .set_work_stealing()
    .build();

// Spawn tasks
let handles: Vec<_> = (0..10)
    .map(|i| pool.spawn(move || println!("Task {} executed", i)))
    .collect();

for handle in handles {
    handle.join().unwrap();
}

pool.shutdown();

§Priority-Based Scheduling

use multipool::ThreadPoolBuilder;

// Create a thread pool with priority-based task scheduling
let pool = ThreadPoolBuilder::new()
    .enable_priority()
    .build();

// Spawn tasks with different priorities
pool.spawn_with_priority(|| println!("Low priority task"), 10);
pool.spawn_with_priority(|| println!("High priority task"), 1);

pool.shutdown();

§Priority Work-Stealing Mode

use multipool::ThreadPoolBuilder;

// Create a thread pool with priority-based work-stealing
let pool = ThreadPoolBuilder::new()
    .set_work_stealing()
    .enable_priority()
    .build();

pool.spawn_with_priority(|| println!("Low priority task"), 10);
pool.spawn_with_priority(|| println!("High priority task"), 1);

pool.shutdown();

§Collecting Metrics

use multipool::{metrics::{ThreadPoolMetrics, AtomicMetricsCollector}, ThreadPoolBuilder};
use std::sync::Arc;

// Create metrics and collector
let metrics = Arc::new(ThreadPoolMetrics::new());
let collector = Arc::new(AtomicMetricsCollector::new(metrics.clone()));

// Create a thread pool with the metrics collector
let pool = ThreadPoolBuilder::new()
    .num_threads(4)
    .with_metrics_collector(collector)
    .build();

// Spawn tasks
for i in 0..5 {
    pool.spawn(move || println!("Task {} executed", i));
}

// Inspect metrics
println!("Queued tasks: {}", metrics.queued_tasks.load(std::sync::atomic::Ordering::SeqCst));
println!("Running tasks: {}", metrics.running_tasks.load(std::sync::atomic::Ordering::SeqCst));
println!("Completed tasks: {}", metrics.completed_tasks.load(std::sync::atomic::Ordering::SeqCst));
println!("Active threads: {}", metrics.active_threads.load(std::sync::atomic::Ordering::SeqCst));

pool.shutdown();

§Using Macros

§spawn_task

Simplifies spawning tasks into the thread pool.

§Example
use multipool::{ThreadPoolBuilder, spawn_task};

let pool = ThreadPoolBuilder::new().build();

// Spawn a task without priority
let handle = spawn_task!(pool, || println!("Task without priority"));

// Spawn a task with priority
let priority_handle = spawn_task!(pool, || println!("Task with priority"), priority: 1); // This should not compile

handle.join().unwrap();
priority_handle.join().unwrap();
pool.shutdown();
§log_metrics

Logs the current metrics of the thread pool, such as queued and running tasks.

§Example
use multipool::{metrics::{ThreadPoolMetrics, AtomicMetricsCollector}, ThreadPoolBuilder, log_metrics};
use std::sync::Arc;

let metrics = Arc::new(ThreadPoolMetrics::new());
let collector = Arc::new(AtomicMetricsCollector::new(metrics.clone()));
let pool = ThreadPoolBuilder::new().with_metrics_collector(collector).build();

log_metrics!(metrics);
pool.shutdown();
§create_thread_pool

Creates a thread pool with various configurations.

§Example
use multipool::create_thread_pool;

let pool = create_thread_pool!(num_threads: 8, work_stealing: true, priority: true);
pool.shutdown();

Re-exports§

pub use pool::ThreadPoolBuilder;

Modules§

metrics
Metrics collection for the thread pool.
pool
Thread pool implementation with configurable task fetching modes.

Macros§

create_thread_pool
Creates a thread pool with various configurations.
log_metrics
Logs the current metrics of the thread pool.
spawn_task
Simplifies spawning tasks into the thread pool.

Functions§

run_traditional
Runs a set of tasks in traditional multi-threading mode (without the thread pool).