Last active
December 17, 2019 12:37
-
-
Save dns2utf8/98e3b9977c23c656788be36cca1f9477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run on play.rust-lang.org