Skip to content

Instantly share code, notes, and snippets.

@dns2utf8
Last active December 17, 2019 12:37
Show Gist options
  • Save dns2utf8/98e3b9977c23c656788be36cca1f9477 to your computer and use it in GitHub Desktop.
Save dns2utf8/98e3b9977c23c656788be36cca1f9477 to your computer and use it in GitHub Desktop.
// Source: https://gist.github.com/dns2utf8/98e3b9977c23c656788be36cca1f9477
use threadpool::Builder; // 1.7.1
fn main() {
let max_jobs = 3;
let pool = Builder::new().num_threads(1).build();
let maybe_submit = |func| {
if pool.queued_count() < max_jobs {
pool.execute(func);
Ok("submitted")
} else {
Err("full")
}
};
let r = (0..10).map(|_| maybe_submit(|| ())).collect::<Vec<_>>();
println!("submit result: {:#?}!", r);
}
@dns2utf8
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment