threadpool example
cargo devel CI / cargo CI (push) Successful in 7m14s Details

This commit is contained in:
Christoph J. Scherr 2024-09-16 10:23:28 +02:00
parent 73193b430d
commit c7975c1859
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
4 changed files with 49 additions and 1 deletions

16
Cargo.lock generated
View File

@ -4661,6 +4661,15 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "threadpool"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa"
dependencies = [
"num_cpus",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.3.36" version = "0.3.36"
@ -4885,6 +4894,13 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
[[package]]
name = "tpdemo"
version = "0.1.0"
dependencies = [
"threadpool",
]
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.40" version = "0.1.40"

View File

@ -30,7 +30,7 @@ members = [
"members/arithmetics", "members/arithmetics",
"members/egui-demo", "members/egui-demo",
"members/rfd-demo", "members/rfd-demo",
"members/iter-prod", "members/iter-prod", "members/tpdemo",
] ]
default-members = [ default-members = [
".", ".",

View File

@ -0,0 +1,7 @@
[package]
name = "tpdemo"
version = "0.1.0"
edition = "2021"
[dependencies]
threadpool = "1.8.1"

View File

@ -0,0 +1,25 @@
use std::sync::{Arc, Mutex};
use threadpool::ThreadPool;
struct Fish(i32);
fn main() {
let thing: Arc<Mutex<Fish>> = Arc::new(Mutex::new(Fish(0)));
let tp = ThreadPool::new(20);
while thing.lock().unwrap().0 < 200 {
let thing = thing.clone();
tp.execute(move || {
do_thing(thing);
});
}
}
fn do_thing(thing: Arc<Mutex<Fish>>) {
println!("blubb");
#[allow(deprecated)]
std::thread::sleep_ms(1000);
thing.lock().unwrap().0 += 1;
}