Expand description
Dynpool is a thread manager that is lightweight, flexible, and rescalable. The pool is designed for minimal overhead, without expensive locks or an extra management thread. Add a job queue yourself, or don’t!
To use dynpool, all you need is an implementation of System
. The pool will
repeatedly call System::work
from many threads, each with a per-thread
data object. Rather than requiring you to rescale the pool from the outside,
dynpool
will constantly query the worker count from System::scale
. This
is actually faster, since a simple scale
implementation can be inlined
into the worker! Your system can be run in the background, and controlled
through the Pool
object, or run in the foreground to make use of the
current thread.
struct Printer(Instant);
impl System for Printer {
type Data = String;
// How many threads? The pool will scale up over time!
fn scale(&self) -> Scale {
let time = self.0.elapsed();
let ms = time.as_secs() * 1000 + time.subsec_millis() as u64;
match ms {
0..=200 => Scale::active(1),
201..=400 => Scale::active(2),
401..=600 => Scale::active(3),
601..=800 => Scale::active(4),
_ => Scale::shutdown(),
}
}
// Pick a string for each thread.
fn init(&self, index: usize) -> String {
match index {
0 => "Hello",
1 => "Hola",
2 => "Bonjour",
3 => "Ciao",
_ => unreachable!(),
}.to_owned()
}
// Do work on several threads!
fn work(&self, text: &mut String) -> Decision {
println!("{}", text);
*text += " Again";
sleep(Duration::from_millis(100));
Decision::Again
}
}
fn main() {
Pool::start_fg(Printer(Instant::now())).unwrap();
println!("This is the end!");
}
There are also builtin functions for concisely altering and constructing systems.
let workers = func_worker(|index| {
println!("New worker #{}", index);
move || {
println!("Hello from #{}", index);
Decision::Again
}
});
let sys = with_threads(workers, 10);
let end_time = Instant::now() + Duration::from_millis(500);
Pool::start_fg(shutdown_after(sys, end_time)).unwrap();
Structs§
- Pool
- A handle to a lightweight thread pool. This is the primary type in the dynpool crate.
- Pool
Paniced Error - This error singles the detection of a worker panic.
Enums§
- Decision
- What should the pool do next?
- Scale
- A goal for the worker pool. This describes the scale that the pool should attempt to reach.
Traits§
- Settable
System - Implementors of this trait are systems with a stored scale.
- System
- Implementors of this trait provide a function to complete work and a function to determine the number of worker threads. This is the primary trait of the dynpool crate.
- System
Work - A subset of
System
that can perform work, but my not provide scale. Should be implemented by the user or created by functions such asfunc_worker
.
Functions§
- cache_
scale - Wrap a system so that
System::scale
is called no more than once per period of time, and is otherwise cached. This should be used if scale calculation is expensive. - dyn_
func_ worker - Begin creating a system which calls the given closure to create a boxed work function for each thread.
- func_
worker - Begin creating a system which calls the given closure to create a work function for each thread.
- shutdown_
after - Create a system wrapper which will shutdown after the provided time.
- simple_
func_ worker - Begin creating a system which calls the given function from each thread, passing the thread’s index.
- with_
scale - Create a system that has an internally stored pool scale.
- with_
threads - Create a system that has a set number of active workers.