使用线程同时运行代码
在大多数当前的操作系统中,已执行程序的代码在进程中运行,操作系统将同时管理多个进程。在一个程序中,你也可以有同时运行的独立部分。运行这些独立部分的功能称为线程。例如,一个 Web 服务器可以有多个线程,以便它可以同时响应多个请求。
将程序中的计算分成多个线程同时运行多个任务可以提高性能,但也会增加复杂性。由于线程可以同时运行,因此无法保证不同线程上代码部分的运行顺序。这可能导致以下问题,例如:
- 竞态条件,其中线程以不一致的顺序访问数据或资源
- 死锁,其中两个线程相互等待,阻止两个线程继续运行
- 仅在某些情况下发生且难以可靠地重现和修复的错误
Rust 试图减轻使用线程的负面影响,但在多线程环境中进行编程仍然需要仔细考虑,并且需要与在单个线程中运行的程序不同的代码结构。
编程语言以几种不同的方式实现线程,许多操作系统提供一个 API,语言可以调用该 API 来创建新线程。Rust 标准库使用线程实现的 1:1 模型,即一个程序每个语言线程使用一个操作系统线程。有一些 crate 实现了其他线程模型,这些模型对 1:1 模型进行了不同的权衡。
使用 spawn
创建新线程
要创建一个新线程,我们调用 thread::spawn
函数,并传递一个包含我们要在新线程中运行的代码的闭包(我们在第 13 章中讨论过闭包)。清单 16-1 中的示例打印来自主线程的一些文本,以及来自新线程的其他文本。
文件名:src/main.rs
use std::thread; use std::time::Duration; fn main() { thread::spawn(|| { for i in 1..10 { println!("hi number {i} from the spawned thread!"); thread::sleep(Duration::from_millis(1)); } }); for i in 1..5 { println!("hi number {i} from the main thread!"); thread::sleep(Duration::from_millis(1)); } }
清单 16-1:创建一个新线程来打印一件事,同时主线程打印其他事
请注意,当 Rust 程序的主线程完成时,所有生成的线程都会关闭,无论它们是否已完成运行。此程序的输出每次可能略有不同,但它将类似于以下内容:
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 main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
对 thread::sleep
的调用强制线程停止执行一小段时间,从而允许不同的线程运行。线程可能会轮流运行,但这并不能保证:这取决于你的操作系统如何调度线程。在此次运行中,主线程首先打印,即使生成线程的打印语句首先出现在代码中。即使我们告诉生成线程打印直到 i
为 9,它在主线程关闭之前也只到了 5。
如果你运行此代码并且只看到主线程的输出,或者看不到任何重叠,请尝试增加范围内的数字,以便为操作系统在线程之间切换创造更多机会。
使用 join
句柄等待所有线程完成
清单 16-1 中的代码不仅因为主线程结束而过早停止了生成线程,而且由于无法保证线程的运行顺序,我们也不能保证生成线程会运行!
我们可以通过将 thread::spawn
的返回值保存在变量中来解决生成线程不运行或过早结束的问题。thread::spawn
的返回类型是 JoinHandle
。JoinHandle
是一个拥有的值,当我们在其上调用 join
方法时,它将等待其线程完成。清单 16-2 展示了如何使用我们在清单 16-1 中创建的线程的 JoinHandle
并调用 join
来确保在 main
退出之前生成线程完成
文件名:src/main.rs
use std::thread; use std::time::Duration; fn main() { let handle = thread::spawn(|| { for i in 1..10 { println!("hi number {i} from the spawned thread!"); thread::sleep(Duration::from_millis(1)); } }); for i in 1..5 { println!("hi number {i} from the main thread!"); thread::sleep(Duration::from_millis(1)); } handle.join().unwrap(); }
清单 16-2:保存来自 thread::spawn
的 JoinHandle
以保证线程运行完成
调用句柄上的 join
会阻塞当前正在运行的线程,直到由句柄表示的线程终止。阻塞一个线程意味着该线程被阻止执行工作或退出。因为我们将对 join
的调用放在主线程的 for
循环之后,所以运行清单 16-2 应产生类似于此的输出
hi number 1 from the main thread!
hi number 2 from the main thread!
hi number 1 from the spawned thread!
hi number 3 from the main thread!
hi number 2 from the spawned thread!
hi number 4 from the main thread!
hi number 3 from the spawned 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!
两个线程继续交替运行,但主线程由于调用了 handle.join()
而等待,并且直到生成线程完成才结束。
但是,让我们看看如果我们将 handle.join()
移动到 main
中的 for
循环之前会发生什么,如下所示
文件名:src/main.rs
use std::thread; use std::time::Duration; fn main() { let handle = thread::spawn(|| { for i in 1..10 { println!("hi number {i} from the spawned thread!"); thread::sleep(Duration::from_millis(1)); } }); handle.join().unwrap(); for i in 1..5 { println!("hi number {i} from the main thread!"); thread::sleep(Duration::from_millis(1)); } }
主线程将等待生成线程完成,然后运行其 for
循环,因此输出将不再交错,如下所示
hi number 1 from the spawned thread!
hi number 2 from the spawned thread!
hi number 3 from the spawned 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!
hi number 1 from the main thread!
hi number 2 from the main thread!
hi number 3 from the main thread!
hi number 4 from the main thread!
诸如 join
的调用位置之类的小细节可能会影响你的线程是否同时运行。
将 move
闭包与线程一起使用
我们通常会将 move
关键字与传递给 thread::spawn
的闭包一起使用,因为闭包将获取其从环境中使用的值的所有权,从而将这些值的所有权从一个线程转移到另一个线程。在 第 13 章的“捕获引用或移动所有权”部分,我们讨论了闭包上下文中的 move
。现在,我们将更多地关注 move
和 thread::spawn
之间的交互。
请注意,在清单 16-1 中,我们传递给 thread::spawn
的闭包不带任何参数:我们没有在生成线程的代码中使用主线程中的任何数据。要在生成线程中使用来自主线程的数据,生成线程的闭包必须捕获它需要的值。清单 16-3 显示了尝试在主线程中创建一个向量并在生成线程中使用它的尝试。但是,这还不起作用,你稍后会看到。
文件名:src/main.rs
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("Here's a vector: {v:?}");
});
handle.join().unwrap();
}
清单 16-3:尝试在另一个线程中使用主线程创建的向量
闭包使用 v
,因此它将捕获 v
并使其成为闭包环境的一部分。因为 thread::spawn
在新线程中运行此闭包,所以我们应该能够在新线程内部访问 v
。但是,当我们编译此示例时,会出现以下错误
$ cargo run
Compiling threads v0.1.0 (file:///projects/threads)
error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function
--> src/main.rs:6:32
|
6 | let handle = thread::spawn(|| {
| ^^ may outlive borrowed value `v`
7 | println!("Here's a vector: {v:?}");
| - `v` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src/main.rs:6:18
|
6 | let handle = thread::spawn(|| {
| __________________^
7 | | println!("Here's a vector: {v:?}");
8 | | });
| |______^
help: to force the closure to take ownership of `v` (and any other referenced variables), use the `move` keyword
|
6 | let handle = thread::spawn(move || {
| ++++
For more information about this error, try `rustc --explain E0373`.
error: could not compile `threads` (bin "threads") due to 1 previous error
Rust 推断如何捕获 v
,并且因为 println!
只需要对 v
的引用,所以闭包尝试借用 v
。但是,这里有一个问题:Rust 无法判断生成线程将运行多久,因此它不知道对 v
的引用是否始终有效。
清单 16-4 提供了一个更有可能具有对 v
的引用的场景,该引用将无效
文件名:src/main.rs
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("Here's a vector: {v:?}");
});
drop(v); // oh no!
handle.join().unwrap();
}
清单 16-4:一个带有闭包的线程,该闭包尝试从丢弃 v
的主线程捕获对 v
的引用
如果 Rust 允许我们运行此代码,则可能会将生成线程立即放入后台而不运行。生成线程内部有对 v
的引用,但主线程会立即使用我们在第 15 章中讨论的 drop
函数删除 v
。然后,当生成线程开始执行时,v
不再有效,因此对它的引用也无效。糟糕!
要修复清单 16-3 中的编译器错误,我们可以使用错误消息的建议
help: to force the closure to take ownership of `v` (and any other referenced variables), use the `move` keyword
|
6 | let handle = thread::spawn(move || {
| ++++
通过在闭包之前添加 move
关键字,我们强制闭包获取它正在使用的值的所有权,而不是允许 Rust 推断它应该借用这些值。清单 16-5 中所示的对清单 16-3 的修改将编译并按我们的意图运行
文件名:src/main.rs
use std::thread; fn main() { let v = vec![1, 2, 3]; let handle = thread::spawn(move || { println!("Here's a vector: {v:?}"); }); handle.join().unwrap(); }
清单 16-5:使用 move
关键字强制闭包获取它使用的值的所有权
我们可能会试图尝试使用相同的方法来修复清单 16-4 中主线程通过使用 move
闭包调用 drop
的代码。但是,此修复程序不起作用,因为清单 16-4 尝试执行的操作因不同的原因而被禁止。如果我们将 move
添加到闭包中,我们会将 v
移动到闭包的环境中,并且我们不能再在主线程中调用 drop
。我们会得到这个编译器错误
$ cargo run
Compiling threads v0.1.0 (file:///projects/threads)
error[E0382]: use of moved value: `v`
--> src/main.rs:10:10
|
4 | let v = vec![1, 2, 3];
| - move occurs because `v` has type `Vec<i32>`, which does not implement the `Copy` trait
5 |
6 | let handle = thread::spawn(move || {
| ------- value moved into closure here
7 | println!("Here's a vector: {v:?}");
| - variable moved due to use in closure
...
10 | drop(v); // oh no!
| ^ value used here after move
For more information about this error, try `rustc --explain E0382`.
error: could not compile `threads` (bin "threads") due to 1 previous error
Rust 的所有权规则再次拯救了我们!我们从清单 16-3 中的代码中得到了一个错误,因为 Rust 是保守的,并且只为线程借用了 v
,这意味着主线程理论上可能会使生成线程的引用无效。通过告诉 Rust 将 v
的所有权转移到生成线程,我们向 Rust 保证主线程不会再使用 v
。如果我们以相同的方式更改清单 16-4,那么当我们在主线程中尝试使用 v
时,我们就会违反所有权规则。move
关键字会覆盖 Rust 的保守默认借用;它不会让我们违反所有权规则。
在对线程和线程 API 有了基本了解之后,让我们来看看我们可以使用线程做些什么。