threadpool example
cargo devel CI / cargo CI (push) Successful in 7m14s
Details
cargo devel CI / cargo CI (push) Successful in 7m14s
Details
This commit is contained in:
parent
73193b430d
commit
c7975c1859
|
@ -4661,6 +4661,15 @@ dependencies = [
|
|||
"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]]
|
||||
name = "time"
|
||||
version = "0.3.36"
|
||||
|
@ -4885,6 +4894,13 @@ version = "0.3.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
|
||||
|
||||
[[package]]
|
||||
name = "tpdemo"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"threadpool",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.40"
|
||||
|
|
|
@ -30,7 +30,7 @@ members = [
|
|||
"members/arithmetics",
|
||||
"members/egui-demo",
|
||||
"members/rfd-demo",
|
||||
"members/iter-prod",
|
||||
"members/iter-prod", "members/tpdemo",
|
||||
]
|
||||
default-members = [
|
||||
".",
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "tpdemo"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
threadpool = "1.8.1"
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue