mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 04:33:27 +08:00
Updated 'src/concurrency/threads'.
This commit is contained in:
6
projects/spawn_demo/Cargo.toml
Normal file
6
projects/spawn_demo/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "spawn_demo"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
18
projects/spawn_demo/src/main.rs
Normal file
18
projects/spawn_demo/src/main.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
let handle = thread::spawn(|| {
|
||||
for i in 1..10 {
|
||||
println! ("hi,生成的线程中的数字 {i} !");
|
||||
thread::sleep(Duration::from_millis(1));
|
||||
}
|
||||
});
|
||||
|
||||
handle.join().unwrap();
|
||||
|
||||
for i in 1..5 {
|
||||
println! ("hi,主线程中的数字 {i} !");
|
||||
thread::sleep(Duration::from_millis(1));
|
||||
}
|
||||
}
|
||||
6
projects/threads/Cargo.toml
Normal file
6
projects/threads/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "threads"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
13
projects/threads/src/main.rs
Normal file
13
projects/threads/src/main.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
let v = vec! [1, 2, 3];
|
||||
|
||||
let handle = thread::spawn(move || {
|
||||
println! ("这里有个矢量值:{v:?}");
|
||||
});
|
||||
|
||||
drop(v);
|
||||
|
||||
handle.join().unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user