Expand description
§ice-threads
ice-threads is a lightweight Thread Pool (Melter) implementation.
User can enqueue Tasks (Ice) to be executed.
Optionally, user can return a value that can be collected through a special handle (Bottle) that is returned by the thread pool task enqueuing method.
Tasks are put into the queue and eventually picked up by a task stealing thread (Heater) and executed.
§Features
- Very simple to use.
Examples
. - Very light crate with no dependencies.
§Examples
Bare minimum:
use ice_threads::Melter;
fn main() {
let melter = Melter::new(1);
let s = melter.melt(|| {
"Watch this task melt away!"
}).open();
println!("{}", s);
}
Full control over result collection method:
use ice_threads::Melter;
fn main() {
let melter = Melter::new(4);
let mut bottles = Vec::new();
let src = "Water is collected synchronously in whatever order you want it to be";
for chr in src.chars() {
bottles.push(melter.melt(
move || {
chr
}
));
}
let water = bottles.iter()
.map(|bottle| { bottle.open() })
.collect::<String>();
println!("{}", water);
}
More thorough example:
use ice_threads::Melter;
fn main() {
let melter = Melter::new(4);
let mut bottles = Vec::new(); // Vec<Bottle<i32>>
for task_id in 0..8i32 { // add a type hint for Rust
// Handle must always be used...
let bottle = melter.melt(move || {
use std::{thread::sleep, time::Duration};
// time consuming task
sleep(Duration::from_secs(1));
println!("Finished task {}", task_id);
task_id
});
// ...and not dropped without using it first
bottles.push(bottle);
}
// results don't need to be used but bottles must be opened
for bottle in bottles {
let result = bottle.open(); // blocking operation
println!("Opened bottle, got: {}", result);
}
}