第一个块中给出的代码有效。
如何将其放入impl
创建单独线程的单独部分中?
就像第二个代码块一样,其中注意到так не работает
.
播放_1
use std::time::{SystemTime, Instant};
use std::thread;
use thread::JoinHandle;
fn main() {
let handle: JoinHandle<(usize, SystemTime, SystemTime, core::time::Duration)> = thread::spawn(|| {
let t21: SystemTime = SystemTime::now();
let start_02 = Instant::now();
let mut j: usize = 0;
loop {
if j == 10 { break; }
j = j + 1;
}
let t22: SystemTime = SystemTime::now();
let d2:(usize, SystemTime, SystemTime, core::time::Duration) = (j, t21, t22, start_02.elapsed());
d2
});
let t11: SystemTime = SystemTime::now();
let start_01 = Instant::now();
let mut i: usize = 0;
loop {
if i == 10 { break; }
i = i + 1;
}
let t12 = SystemTime::now();
let d2 = handle.join().expect("thread panicked :...");
println!("i = {:?} \nt11 = {:?} \nt12 = {:?}", i, t11, t12);
println!("dt_01 = {:?}\n", start_01.elapsed());
//
println!("j = {:?} \nt21 = {:?} \nt22 = {:?}", &d2.0, &d2.1, &d2.2);
println!("dt_02 = {:?}\n", &d2.3);
}
像这样的东西:
play_2
use std::time::{SystemTime, Instant};
use std::thread;
use thread::JoinHandle;
struct Thd {
a: usize,
b: SystemTime,
c: SystemTime,
d: core::time::Duration,
}
impl Thd {
pub fn new() -> JoinHandle<(usize, SystemTime, SystemTime, core::time::Duration)> {
thread::spawn(|| {
let t21: SystemTime = SystemTime::now();
let start_02 = Instant::now();
let mut j: usize = 0;
loop {
if j == 1_000_000 { break; }
j = j + 1;
}
let t22: SystemTime = SystemTime::now();
let d: Thd = Thd01 {a: j, b: t21, c: t22, d:start_02.elapsed()};
let d2:(usize, SystemTime, SystemTime, core::time::Duration) = (d.a, d.b, d.c, d.d);
d2
});
}
}
fn main() {
// Error `так не работает`:
let handle: JoinHandle<(usize, SystemTime, SystemTime, core::time::Duration)> = Thd::new();
let t11: SystemTime = SystemTime::now();
let start_01 = Instant::now();
let mut i: usize = 0;
loop {
if i == 1_000_000 { break; }
i = i + 1;
}
let t12 = SystemTime::now();
let d2 = handle.join().expect("thread panicked :...");
println!("i = {:?} \nt11 = {:?} \nt12 = {:?}", i, t11, t12);
println!("dt_01 = {:?}\n", start_01.elapsed());
//
println!("j = {:?} \nt21 = {:?} \nt22 = {:?}", &d2.0, &d2.1, &d2.2);
println!("dt_02 = {:?}\n", &d2.3);
}
更新_01
use std::time::{SystemTime, Instant};
use std::thread;
use thread::JoinHandle;
struct Thd {
a: usize,
b: SystemTime,
c: SystemTime,
dt: core::time::Duration,
}
impl Thd {
pub fn new() -> JoinHandle<(usize, SystemTime, SystemTime, core::time::Duration)> {
let handle: JoinHandle<(usize, SystemTime, SystemTime, core::time::Duration)> =
thread::spawn(|| {
let t21: SystemTime = SystemTime::now();
let start_02 = Instant::now();
let mut j: usize = 0;
loop {
if j == 1_000_000 { break; }
j = j + 1;
}
let t22: SystemTime = SystemTime::now();
let d: Thd = Thd {a: j, b: t21, c: t22, dt:start_02.elapsed()};
let d2:(usize, SystemTime, SystemTime, core::time::Duration) = (d.a, d.b, d.c, d.dt);
d2
});
handle
}
}
fn main() {
// `так работает`:
let handle: JoinHandle<(usize, SystemTime, SystemTime, core::time::Duration)> = Thd::new();
let t11: SystemTime = SystemTime::now();
let start_01 = Instant::now();
let mut i: usize = 0;
loop {
if i == 1_000_000 { break; }
i = i + 1;
}
let t12 = SystemTime::now();
let d2 = handle.join().expect("thread panicked :...");
println!("i = {:?} \nt11 = {:?} \nt12 = {:?}", i, t11, t12);
println!("dt_01 = {:?}\n", start_01.elapsed());
//
println!("j = {:?} \nt21 = {:?} \nt22 = {:?}", &d2.0, &d2.1, &d2.2);
println!("dt_02 = {:?}\n", &d2.3);
}
/*
i = 1000000
t11 = SystemTime { tv_sec: 1737389697, tv_nsec: 383198522 }
t12 = SystemTime { tv_sec: 1737389697, tv_nsec: 385890160 }
dt_01 = 2.931694ms
j = 1000000
t21 = SystemTime { tv_sec: 1737389697, tv_nsec: 383252593 }
t22 = SystemTime { tv_sec: 1737389697, tv_nsec: 386045474 }
dt_02 = 2.792891ms
*/
考虑到
Thd
这不是线程本身,而是其工作的结果,特别是因为它可以在不创建线程的情况下使用,因此从实现中完全排除线程的提及并重命名它是有意义的,并稍微概括一下。沙盒