Dynpool
Dynpool is a thread manager that is lightweight, flexible, and rescalable.
Implement System
, pass it off to a Pool
, and see the threads go!
extern crate dynpool;
use dynpool::*;
use std::thread::sleep;
use std::time::{Duration, Instant};
struct Printer(Instant);
impl System for Printer {
type Data = String;
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(),
}
}
fn init(&self, index: usize) -> String {
match index {
0 => "Hello",
1 => "Hola",
2 => "Bonjour",
3 => "Ciao",
_ => unreachable!(),
}.to_owned()
}
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!");
}
You can also use built-in systems.
Pool::start_fg(
shutdown_after(fixed_threads(shared_func_worker(
|index| println!("Hello from thread #{}", index)
), 10), time)
).unwrap();