2017-03-18 21:39:27 +08:00
<!DOCTYPE HTML>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title > 线程 - Rust 程序设计语言 简体中文版< / title >
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "Rust 程序设计语言 简体中文版" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< base href = "" >
< link rel = "stylesheet" href = "book.css" >
< link href = 'https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel = 'stylesheet' type = 'text/css' >
< link rel = "shortcut icon" href = "favicon.png" >
<!-- Font Awesome -->
< link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" >
< link rel = "stylesheet" href = "highlight.css" >
< link rel = "stylesheet" href = "tomorrow-night.css" >
<!-- MathJax -->
< script type = "text/javascript" src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" > < / script >
<!-- Fetch JQuery from CDN but have a local fallback -->
< script src = "https://code.jquery.com/jquery-2.1.4.min.js" > < / script >
< script >
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
}
< / script >
< / head >
< body class = "light" >
<!-- Set the theme before any content is loaded, prevents flash -->
< script type = "text/javascript" >
var theme = localStorage.getItem('theme');
if (theme == null) { theme = 'light'; }
$('body').removeClass().addClass(theme);
< / script >
<!-- Hide / unhide sidebar before it is displayed -->
< script type = "text/javascript" >
var sidebar = localStorage.getItem('sidebar');
if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
< / script >
< div id = "sidebar" class = "sidebar" >
2017-04-05 23:13:49 +08:00
< ul class = "chapter" > < li > < a href = "ch01-00-introduction.html" > < strong > 1.< / strong > 介绍< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch01-01-installation.html" > < strong > 1.1.< / strong > 安装< / a > < / li > < li > < a href = "ch01-02-hello-world.html" > < strong > 1.2.< / strong > Hello, World!< / a > < / li > < / ul > < / li > < li > < a href = "ch02-00-guessing-game-tutorial.html" > < strong > 2.< / strong > 猜猜看教程< / a > < / li > < li > < a href = "ch03-00-common-programming-concepts.html" > < strong > 3.< / strong > 通用编程概念< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch03-01-variables-and-mutability.html" > < strong > 3.1.< / strong > 变量和可变性< / a > < / li > < li > < a href = "ch03-02-data-types.html" > < strong > 3.2.< / strong > 数据类型< / a > < / li > < li > < a href = "ch03-03-how-functions-work.html" > < strong > 3.3.< / strong > 函数如何工作< / a > < / li > < li > < a href = "ch03-04-comments.html" > < strong > 3.4.< / strong > 注释< / a > < / li > < li > < a href = "ch03-05-control-flow.html" > < strong > 3.5.< / strong > 控制流< / a > < / li > < / ul > < / li > < li > < a href = "ch04-00-understanding-ownership.html" > < strong > 4.< / strong > 认识所有权< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch04-01-what-is-ownership.html" > < strong > 4.1.< / strong > 什么是所有权< / a > < / li > < li > < a href = "ch04-02-references-and-borrowing.html" > < strong > 4.2.< / strong > 引用 & 借用< / a > < / li > < li > < a href = "ch04-03-slices.html" > < strong > 4.3.< / strong > Slices< / a > < / li > < / ul > < / li > < li > < a href = "ch05-00-structs.html" > < strong > 5.< / strong > 结构体< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch05-01-method-syntax.html" > < strong > 5.1.< / strong > 方法语法< / a > < / li > < / ul > < / li > < li > < a href = "ch06-00-enums.html" > < strong > 6.< / strong > 枚举和模式匹配< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch06-01-defining-an-enum.html" > < strong > 6.1.< / strong > 定义枚举< / a > < / li > < li > < a href = "ch06-02-match.html" > < strong > 6.2.< / strong > < code > match< / code > 控制流运算符< / a > < / li > < li > < a href = "ch06-03-if-let.html" > < strong > 6.3.< / strong > < code > if let< / code > 简单控制流< / a > < / li > < / ul > < / li > < li > < a href = "ch07-00-modules.html" > < strong > 7.< / strong > 模块< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch07-01-mod-and-the-filesystem.html" > < strong > 7.1.< / strong > < code > mod< / code > 和文件系统< / a > < / li > < li > < a href = "ch07-02-controlling-visibility-with-pub.html" > < strong > 7.2.< / strong > 使用< code > pub< / code > 控制可见性< / a > < / li > < li > < a href = "ch07-03-importing-names-with-use.html" > < strong > 7.3.< / strong > 使用< code > use< / code > 导入命名< / a > < / li > < / ul > < / li > < li > < a href = "ch08-00-common-collections.html" > < strong > 8.< / strong > 通用集合类型< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch08-01-vectors.html" > < strong > 8.1.< / strong > vector< / a > < / li > < li > < a href = "ch08-02-strings.html" > < strong > 8.2.< / strong > 字符串< / a > < / li > < li > < a href = "ch08-03-hash-maps.html" > < strong > 8.3.< / strong > 哈希 map< / a > < / li > < / ul > < / li > < li > < a href = "ch09-00-error-handling.html" > < strong > 9.< / strong > 错误处理< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch09-01-unrecoverable-errors-with-panic.html" > < strong > 9.1.< / strong > < code > panic!< / code > 与不可恢复的错误< / a > < / li > < li > < a href = "ch09-02-recoverable-errors-with-result.html" > < strong > 9.2.< / strong > < code > Result< / code > 与可恢复的错误< / a > < / li > < li > < a href = "ch09-03-to-panic-or-not-to-panic.html" > < strong > 9.3.< / strong > < code > panic!< / code > 还是不< code > panic!< / code > < / a > < / li > < / ul > < / li > < li > < a href = "ch10-00-generics.html" > < strong > 10.< / strong > 泛型、trait 和生命周期< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch10-01-syntax.html" > < strong > 10.1.< / strong > 泛型数据类型< / a > < / li > < li > < a href = "ch10-02-traits.html" > < strong > 10.2.< / strong > trait: 定义共享的行为< / a > < / li > < li > < a href = "ch10-03-lifetime-syntax.html" > < strong > 10.3.< / strong > 生命周期与引用有效性< / a > < / li > < / ul > < / li > < li > < a href = "ch11-00-testing.html" > < strong > 11.< / strong > 测试< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch11-01-writing-tests.html" > < strong > 11.1.< / strong > 编写测试< / a > < / li > < li > < a href = "ch11-02-running-tests.html" > < strong > 11.2.< / strong > 运行测试< / a > < / li > < li > < a href = "ch11-03-test-organization.html" > < strong > 11.3.< / strong > 测试的组织结构< / a > < / li > < / ul > < / li > < li > < a href = "ch12-00-an-io-project.h
2017-03-18 21:39:27 +08:00
< / div >
< div id = "page-wrapper" class = "page-wrapper" >
< div class = "page" >
< div id = "menu-bar" class = "menu-bar" >
< div class = "left-buttons" >
< i id = "sidebar-toggle" class = "fa fa-bars" > < / i >
< i id = "theme-toggle" class = "fa fa-paint-brush" > < / i >
< / div >
< h1 class = "menu-title" > Rust 程序设计语言 简体中文版< / h1 >
< div class = "right-buttons" >
< i id = "print-button" class = "fa fa-print" title = "Print this book" > < / i >
< / div >
< / div >
< div id = "content" class = "content" >
< a class = "header" href = "#使用线程同时运行代码" name = "使用线程同时运行代码" > < h2 > 使用线程同时运行代码< / h2 > < / a >
< blockquote >
< p > < a href = "https://github.com/rust-lang/book/blob/master/second-edition/src/ch16-01-threads.md" > ch16-01-threads.md< / a >
< br >
commit 55b294f20fc846a13a9be623bf322d8b364cee77< / p >
< / blockquote >
< p > 在今天使用的大部分操作系统中,当程序执行时,操作系统运行代码的上下文称为< strong > 进程< / strong > ( < em > process< / em > )。操作系统可以运行很多进程,而操作系统也管理这些进程使得多个程序可以在电脑上同时运行。< / p >
< p > 我们可以将每个进程运行一个程序的概念再往下抽象一层:程序也可以在其上下文中同时运行独立的部分。这个功能叫做< strong > 线程< / strong > ( < em > thread< / em > )。< / p >
2017-03-20 23:47:47 +08:00
< p > 将程序需要执行的计算拆分到多个线程中可以提高性能,因为程序可以在同时进行很多工作。不过使用线程会增加程序复杂性。因为线程是同时运行的,所以无法预先保证不同线程中的代码的执行顺序。这可能会由于线程以不一致的顺序访问数据或资源而导致竞争状态,或由于两个线程相互阻止对方继续运行而造成死锁,以及仅仅出现于特定场景并难以稳定重现的 bug。Rust 减少了这些或那些使用线程的负面影响,不过在多线程上下文中编程仍然需要以与只期望在单个线程中编程不同的方式思考和组织代码。< / p >
< p > 编程语言有一些不同的方法来实现线程。很多操作系统提供了创建新线程的 API。另外, 很多编程语言提供了自己的特殊的线程实现。编程语言提供的线程有时被称作< strong > 轻量级< / strong > ( < em > lightweight< / em > )或< strong > 绿色< / strong > ( < em > green< / em > )线程。这些语言将一系列绿色线程放入不同数量的操作系统线程中执行。因为这个原因,语言调用操作系统 API 创建线程的模型有时被称为 < em > 1:1< / em > ,一个 OS 线程对应一个语言线程。绿色线程模型被称为 < em > M:N< / em > 模型,< code > M< / code > 个绿色线程对应< code > N< / code > 个 OS 线程,这里< code > M< / code > 和< code > N< / code > 不必相同。< / p >
< p > 每一个模型都有其自己的优势和取舍。对于 Rust 来说最重要的取舍是运行时支持。< strong > 运行时< / strong > 是一个令人迷惑的概念;在不同上下文中它可能有不同的含义。这里其代表二进制文件中包含的语言自身的代码。对于一些语言,这些代码是庞大的,另一些则很小。通俗的说,“没有运行时”通常被人们用来指代“小运行时”,因为任何非汇编语言都存在一定数量的运行时。更小的运行时拥有更少的功能不过其优势在于更小的二进制输出。更小的二进制文件更容易在更多上下文中与其他语言结合。虽然很多语言觉得增加运行时来换取更多功能没有什么问题,但是 Rust 需要做到几乎没有运行时,同时不能在为了维持性能而能够在 C 语言中调用方面做出妥协。< / p >
< p > 绿色线程模型功能要求更大的运行时来管理这些线程。为此, Rust 标准库只提供了 1:1 线程模型实现。因为 Rust 是这么一个底层语言,所以有相应的 crate 实现了 M:N 线程模型,如果你宁愿牺牲性能来换取例如更多的线程控制和更低的上下文切换消耗。< / p >
< p > 现在我们明白了 Rust 中的线程是如何定义的,让我们开始探索如何使用标准库提供的线程相关的 API吧。< / p >
< a class = "header" href = "#使用spawn创建新线程" name = "使用spawn创建新线程" > < h3 > 使用< code > spawn< / code > 创建新线程< / h3 > < / a >
< p > 为了创建一个新线程,调用< code > thread::spawn< / code > 函数并传递一个闭包(第十三章学习了闭包),它包含希望在新线程运行的代码。列表 16-1 中的例子在新线程中打印了一些文本而其余的文本在主线程中打印:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > use std::thread;
fn main() {
thread::spawn(|| {
for i in 1..10 {
println!(" hi number {} from the spawned thread!" , i);
}
});
for i in 1..5 {
println!(" hi number {} from the main thread!" , i);
}
}
< / code > < / pre >
< p > < span class = "caption" > Listing 16-1: Creating a new thread to print one thing
while the main thread is printing something else< / span > < / p >
< p > 注意这个函数编写的方式,当主线程结束时,它也会停止新线程。这个程序的输出每次可能都略微不同,不过它大体上看起来像这样:< / p >
< pre > < code class = "language-text" > 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!
< / code > < / pre >
< p > 这些线程可能会轮流运行,不过并不保证如此。在这里,主线程先行打印,即便新创建线程的打印语句位于程序的开头。甚至即便我们告诉新建的线程打印直到< code > i< / code > 等于 9 ,它在主线程结束之前也只打印到了 5。如果你只看到了一个线程, 或没有出现重叠打印的现象, 尝试增加 range 的数值来增加线程暂停并切换到其他线程运行的机会。< / p >
< a class = "header" href = "#使用join等待所有线程结束" name = "使用join等待所有线程结束" > < h4 > 使用< code > join< / code > 等待所有线程结束< / h4 > < / a >
< p > 由于主线程先于新建线程结束,不仅列表 16-1 中的代码大部分时候不能保证新建线程执行完毕,甚至不能实际保证新建线程会被执行!可以通过保存< code > thread::spawn< / code > 的返回值来解决这个问题,这是一个< code > JoinHandle< / code > 。这看起来如列表 16-2 所示:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > use std::thread;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!(" hi number {} from the spawned thread!" , i);
}
});
for i in 1..5 {
println!(" hi number {} from the main thread!" , i);
}
handle.join();
}
< / code > < / pre >
< p > < span class = "caption" > Listing 16-2: Saving a < code > JoinHandle< / code > from < code > thread::spawn< / code >
to guarantee the thread is run to completion< / span > < / p >
< p > < code > JoinHandle< / code > 是一个拥有所有权的值,它可以等待一个线程结束,这也正是< code > join< / code > 方法所做的。通过调用这个句柄的< code > join< / code > ,当前线程会阻塞直到句柄所代表的线程结束。因为我们将< code > join< / code > 调用放在了主线程的< code > for< / code > 循环之后,运行这个例子将产生类似这样的输出:< / p >
< pre > < code > 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!
< / code > < / pre >
< p > 这两个线程仍然会交替执行,不过主线程会由于< code > handle.join()< / code > 调用会等待直到新建线程执行完毕。< / p >
< p > 如果将< code > handle.join()< / code > 放在主线程的< code > for< / code > 循环之前,像这样:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > use std::thread;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!(" hi number {} from the spawned thread!" , i);
}
});
handle.join();
for i in 1..5 {
println!(" hi number {} from the main thread!" , i);
}
}
< / code > < / pre >
< p > 主线程会等待直到新建线程执行完毕之后才开始执行< code > for< / code > 循环,所以输出将不会交替出现:< / p >
< pre > < code > 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!
< / code > < / pre >
< p > 稍微考虑一下将< code > join< / code > 放置与何处会影响线程是否同时运行。< / p >
< a class = "header" href = "#线程和move闭包" name = "线程和move闭包" > < h3 > 线程和< code > move< / code > 闭包< / h3 > < / a >
< p > 第十三章有一个我们没有讲到的闭包功能,它经常用于< code > thread::spawn< / code > : < code > move< / code > 闭包。第十三章中讲到:< / p >
< blockquote >
< p > 获取他们环境中值的闭包主要用于开始新线程的场景< / p >
< / blockquote >
< p > 现在我们正在创建新线程,所以让我们讨论一下获取环境值的闭包吧!< / p >
< p > 注意列表 16-1 中传递给< code > thread::spawn< / code > 的闭包并没有任何参数:并没有在新建线程代码中使用任何主线程的数据。为了在新建线程中使用来自于主线程的数据,需要新建线程的闭包获取它需要的值。列表 16-3 展示了一个尝试在主线程中创建一个 vector 并用于新建线程的例子,不过这么写还不能工作:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!(" Here's a vector: {:?}" , v);
});
handle.join();
}
< / code > < / pre >
< p > < span class = "caption" > Listing 16-3: Attempting to use a vector created by the
main thread from another thread< / span > < / p >
< p > 闭包使用了< code > v< / code > ,所以闭包会获取< code > v< / code > 并使其成为闭包环境的一部分。因为< code > thread::spawn< / code > 在一个新线程中运行这个闭包,所以可以在新线程中访问< code > v< / code > 。< / p >
< p > 然而当编译这个例子时,会得到如下错误:< / p >
< pre > < code > error[E0373]: closure may outlive the current function, but it borrows `v`,
which is owned by the current function
-->
|
6 | let handle = thread::spawn(|| {
| ^^ may outlive borrowed value `v`
7 | println!(" Here's a vector: {:?}" , v);
| - `v` is borrowed here
|
help: to force the closure to take ownership of `v` (and any other referenced
variables), use the `move` keyword, as shown:
| let handle = thread::spawn(move || {
< / code > < / pre >
< p > 当在闭包环境中获取某些值时, Rust 会尝试推断如何获取它。< code > println!< / code > 只需要< code > v< / code > 的一个引用,所以闭包尝试借用< code > v< / code > 。但是这有一个问题:我们并不知道新建线程会运行多久,所以无法知道< code > v< / code > 是否一直时有效的。< / p >
< p > 考虑一下列表 16-4 中的代码,它展示了一个< code > v< / code > 的引用很有可能不再有效的场景:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > 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();
}
< / code > < / pre >
< p > < span class = "caption" > Listing 16-4: A thread with a closure that attempts to
capture a reference to < code > v< / code > from a main thread that drops < code > v< / code > < / span > < / p >
< p > 这些代码可以运行,而新建线程则可能直接就出错了并完全没有机会运行。新建线程内部有一个< code > v< / code > 的引用,不过主线程仍在执行:它立刻丢弃了< code > v< / code > ,使用了第十五章提到的显式丢弃其参数的< code > drop< / code > 函数。接着,新建线程开始执行,现在< code > v< / code > 是无效的了,所以它的引用也就是无效得的。噢,这太糟了!< / p >
< p > 为了修复这个问题,我们可以听取错误信息的建议:< / p >
< pre > < code > help: to force the closure to take ownership of `v` (and any other referenced
variables), use the `move` keyword, as shown:
| let handle = thread::spawn(move || {
< / code > < / pre >
< p > 通过在闭包之前增加< code > move< / code > 关键字,我们强制闭包获取它使用的值的所有权,而不是引用借用。列表 16-5 中展示的对列表 16-3 代码的修改可以按照我们的预期编译并运行:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(move || {
println!(" Here's a vector: {:?}" , v);
});
handle.join();
}
< / code > < / pre >
< p > < span class = "caption" > Listing 16-5: Using the < code > move< / code > keyword to force a closure
to take ownership of the values it uses< / span > < / p >
< p > 那么列表 16-4 中那个主线程调用了< code > drop< / code > 的代码该怎么办呢?如果在闭包上增加了< code > move< / code > ,就将< code > v< / code > 移动到了闭包的环境中,我们将不能对其调用< code > drop< / code > 了。相反会出现这个编译时错误:< / p >
< pre > < code > error[E0382]: use of moved value: `v`
-->
|
6 | let handle = thread::spawn(move || {
| ------- value moved (into closure) here
...
10 | drop(v); // oh no!
| ^ value used here after move
|
= note: move occurs because `v` has type `std::vec::Vec< i32> `, which does
not implement the `Copy` trait
< / code > < / pre >
< p > Rust 的所有权规则又一次帮助了我们!< / p >
< p > 现在我们有一个线程和线程 API 的基本了解,让我们讨论一下使用线程实际可以< strong > 做< / strong > 什么吧。< / p >
2017-03-18 21:39:27 +08:00
< / div >
<!-- Mobile navigation buttons -->
< a href = "ch16-00-concurrency.html" class = "mobile-nav-chapters previous" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "ch16-02-message-passing.html" class = "mobile-nav-chapters next" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
< a href = "ch16-00-concurrency.html" class = "nav-chapters previous" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "ch16-02-message-passing.html" class = "nav-chapters next" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
<!-- Local fallback for Font Awesome -->
< script >
if ($(".fa").css("font-family") !== "FontAwesome") {
$('< link rel = "stylesheet" type = "text/css" href = "_FontAwesome/css/font-awesome.css" > ').prependTo('head');
}
< / script >
<!-- Livereload script (if served using the cli tool) -->
< script src = "highlight.js" > < / script >
< script src = "book.js" > < / script >
< / body >
< / html >