2017-03-18 21:39:27 +08:00
<!DOCTYPE HTML>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title > 可扩展的并发:`Sync`和`Send` - 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" >
2017-03-22 15:30:00 +08:00
< a class = "header" href = "#使用sync和send-trait-的可扩展并发" name = "使用sync和send-trait-的可扩展并发" > < h2 > 使用< code > Sync< / code > 和< code > Send< / code > trait 的可扩展并发< / h2 > < / a >
< blockquote >
< p > < a href = "https://github.com/rust-lang/book/blob/master/second-edition/src/ch16-04-extensible-concurrency-sync-and-send.md" > ch16-04-extensible-concurrency-sync-and-send.md< / a >
< br >
commit 55b294f20fc846a13a9be623bf322d8b364cee77< / p >
< / blockquote >
< p > Rust 的并发模型中一个有趣的方面是语言本身对并发知道的< strong > 很少< / strong > 。我们讨论过的几乎所有内容都是标准库的一部分,而不是语言本身的内容。因为并不需要语言提供任何用于并发上下文中的内容,并发选择也不仅限于标准库或语言所提供的:我们可以编写自己的或使用别人编写的内容。< / p >
< p > 我们说了< strong > 几乎< / strong > 所有内容都不在语言本身,那么位于语言本身的是什么呢?这是两个 trait, 都位于< code > std::marker< / code > : < code > Sync< / code > 和< code > Send< / code > 。< / p >
< a class = "header" href = "#send用于表明所有权可能被传送给其他线程" name = "send用于表明所有权可能被传送给其他线程" > < h3 > < code > Send< / code > 用于表明所有权可能被传送给其他线程< / h3 > < / a >
< p > < code > Send< / code > 标记 trait 表明类型的所有权可能被在线程间传递。几乎所有的 Rust 类型都是< code > Send< / code > 的,不过有一些例外。标准库中提供的一个不是< code > Send< / code > 的类型是< code > Rc< T> < / code > :如果克隆< code > Rc< T> < / code > 值并尝试将克隆的所有权传递给另一个线程,这两个线程可能会同时更新引用计数。正如上一部分提到的,< code > Rc< T> < / code > 被实现为用于单线程场景,这时不需要为拥有线程安全的引用计数而付出性能代价。< / p >
< p > 因为< code > Rc< T> < / code > 没有标记为< code > Send< / code > , Rust 的类型系统和 trait bound 会确保我们永远也不会忘记或错误的把一个< code > Rc< T> < / code > 值不安全的在线程间传递。列表 16-14 曾尝试这么做,不过得到了一个错误说< code > the trait Send is not implemented for Rc< Mutex< i32> > < / code > 。当切换为标记为< code > Send< / code > 的< code > Arc< T> < / code > 时,代码就可以编译了。< / p >
< p > 任何完全由< code > Send< / code > 的类型组成的类型也会自动被标记为< code > Send< / code > 。几乎所有基本类型都是< code > Send< / code > 的, 除了第十九章将会讨论的裸指针( raw pointer) 之外。大部分标准库类型是< code > Send< / code > 的,除了< code > Rc< T> < / code > 之外。< / p >
< a class = "header" href = "#sync表明多线程访问是安全的" name = "sync表明多线程访问是安全的" > < h3 > < code > Sync< / code > 表明多线程访问是安全的< / h3 > < / a >
< p > < code > Sync< / code > 标记 trait 表明一个类型可以安全的在多个线程中拥有其值的引用。换一种方式来说就是对于任意类型< code > T< / code > ,如果< code > & T< / code > ( < code > T< / code > 的引用)是< code > Send< / code > 的话< code > T< / code > 就是< code > Sync< / code > 的,这样其引用就可以安全的发送到另一个线程。类似于< code > Send< / code > 的情况,基本类型是< code > Sync< / code > 的,完全由< code > Sync< / code > 的类型组成的类型也是< code > Sync< / code > 的。< / p >
< p > < code > Rc< T> < / code > 也不是< code > Sync< / code > 的,出于其不是< code > Send< / code > 的相同的原因。< code > RefCell< T> < / code > (第十五章讨论过)和< code > Cell< T> < / code > 系列类型不是< code > Sync< / code > 的。< code > RefCell< T> < / code > 在运行时所进行的借用检查也不是线程安全的。< code > Mutex< T> < / code > 是< code > Sync< / code > 的,正如上一部分所讲的它可以被用来在多线程中共享访问。< / p >
< a class = "header" href = "#手动实现send和sync是不安全的" name = "手动实现send和sync是不安全的" > < h3 > 手动实现< code > Send< / code > 和< code > Sync< / code > 是不安全的< / h3 > < / a >
< p > 通常并不需要实现< code > Send< / code > 和< code > Sync< / code > trait, 因为由是< code > Send< / code > 和< code > Sync< / code > 的类型组成的类型也自动就是< code > Send< / code > 和< code > Sync< / code > 的了。因为他们是标记 trait, 甚至都不需要实现任何方法。他们只是用来加强并发相关的不可变行性的。< / p >
< p > 实现这些标记 trait 涉及到实现不安全的 Rust 代码。第十九章将会讲到如何使用不安全 Rust 代码;现在,重要的是在创建新的由不是< code > Send< / code > 和< code > Sync< / code > 的部分构成的并发类型时需要多加小心,以确保维持其安全保证。< a href = "https://doc.rust-lang.org/stable/nomicon/vec.html" > The Nomicon< / a > 中由更多关于这些保证和如何维持他们的信息。< / p >
< a class = "header" href = "#总结" name = "总结" > < h2 > 总结< / h2 > < / a >
< p > 这不会是本书最后一个出现并发的章节;第二十章的项目会在更现实的场景中使用这些概念,而不像本章中讨论的这些小例子。< / p >
< p > 正如我们提到的,因为 Rust 本身很少有处理并发的部分内容,有很多的并发方案都由 crate 实现。他们比标准库要发展的更快;请在网上搜索当前最新的用于多线程场景的 crate。< / p >
< p > Rust 提供了用于消息传递的通道,和像< code > Mutex< T> < / code > 和< code > Arc< T> < / code > 这样可以安全的用于并发上下文的智能指针。类型系统和借用检查器会确保这些场景中的代码不会出现数据竞争和无效的引用。一旦代码可以编译了,我们就可以坚信这些代码可以正确的运行于多线程环境,而不会出现其他语言中经常出现的那些难以追踪的 bug。并发编程不再是什么可怕的概念: 无所畏惧的使你的程序使用并发吧! < / p >
< p > 接下来,让我们讨论一下当 Rust 程序变得更大时那些符合习惯的模拟问题和结构的解决方案,以及 Rust 风格如何与面向对象编程( Object Oriented Programming) 中那些你所熟悉的概念相联系。< / p >
2017-03-18 21:39:27 +08:00
< / div >
<!-- Mobile navigation buttons -->
< a href = "ch16-03-shared-state.html" class = "mobile-nav-chapters previous" >
< i class = "fa fa-angle-left" > < / i >
< / a >
2017-04-05 23:13:49 +08:00
< a href = "ch17-00-oop.html" class = "mobile-nav-chapters next" >
< i class = "fa fa-angle-right" > < / i >
< / a >
2017-03-18 21:39:27 +08:00
< / div >
< a href = "ch16-03-shared-state.html" class = "nav-chapters previous" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-left" > < / i >
< / a >
2017-04-05 23:13:49 +08:00
< a href = "ch17-00-oop.html" class = "nav-chapters next" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-right" > < / i >
< / a >
2017-03-18 21:39:27 +08:00
< / 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 >