手册
目录
收藏526
分享
阅读11133
更新时间2025-08-27
在并发编程中,程序的不同部分独立执行。 另一方面,在并行编程中,程序的不同部分同时执行。 随着越来越多的计算机利用其多个处理器,这两种模型同等重要。
我们可以使用线程来同时运行代码。 在当前的操作系统中,执行程序的代码在一个进程中运行,并且操作系统同时管理多个进程。 在您的程序中,您还可以拥有同时运行的独立部分。 运行这些独立部分的功能称为线程。
thread::spawn函数用于创建一个新线程。 spawn 函数采用闭包作为参数。 闭包定义了线程应该执行的代码。 以下示例从主线程打印一些文本,并从新线程打印其他文本。
//导入必要的模块
use std::thread;
use std::time::Duration;
fn main() {
//创建一个新线程
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
//主线程执行的代码
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
hi number 1 from the main thread! hi number 1 from the spawned thread! hi number 2 from the main thread! hi number 2 from the spawned thread! hi number 3 from the main thread! hi number 3 from the spawned thread! hi number 4 from the spawned thread! hi number 4 from the main thread!
主线程打印从 1 到 4 的值。
注意 − 当主线程结束时,新线程将停止。 该程序的输出每次可能会略有不同。
thread::sleep 函数强制线程在短时间内停止执行,从而允许另一个线程运行。 线程可能会轮流,但这并不能保证——这取决于操作系统如何调度线程。 在此运行中,首先打印主线程,即使来自生成线程的打印语句首先出现在代码中。 此外,即使生成的线程被编程为打印值直到 9,它也只能在主线程关闭之前打印到 5。
生成的线程可能没有机会运行或完全运行。 这是因为主线程完成得很快。 函数 spawn
use std::thread;
use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
handle.join().unwrap();
}
hi number 1 from the main thread! hi number 1 from the spawned thread! hi number 2 from the spawned thread! hi number 2 from the main thread! hi number 3 from the spawned thread! hi number 3 from the main thread! hi number 4 from the main thread! hi number 4 from the spawned thread! hi number 5 from the spawned thread! hi number 6 from the spawned thread! hi number 7 from the spawned thread! hi number 8 from the spawned thread! hi number 9 from the spawned thread!
主线程和派生线程继续切换。
注意 − 由于调用 join() 方法,主线程等待生成的线程完成。
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习