Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

无畏并发

Fearless Concurrency

安全且高效地处理并发编程是 Rust 的另一个主要目标。并发编程(Concurrent programming)指程序的各个部分独立执行,而并行编程(parallel programming)指程序的各个部分同时执行。随着越来越多的计算机开始利用其多处理器的优势,这两种编程方式正变得日益重要。从历史上看,在这些语境下编程一直是困难且容易出错的。Rust 希望改变这一现状。

Handling concurrent programming safely and efficiently is another of Rust’s major goals. Concurrent programming, in which different parts of a program execute independently, and parallel programming, in which different parts of a program execute at the same time, are becoming increasingly important as more computers take advantage of their multiple processors. Historically, programming in these contexts has been difficult and error-prone. Rust hopes to change that.

最初,Rust 团队认为确保内存安全和防止并发问题是两个需要用不同方法解决的独立挑战。随着时间的推移,团队发现所有权和类型系统是帮助管理内存安全以及并发问题的强大工具集!通过利用所有权和类型检查,许多并发错误在 Rust 中是编译时错误,而不是运行时错误。因此,与其让你花大量时间尝试重现导致运行时并发 bug 的确切情况,错误的代码将无法通过编译,并会显示解释问题的错误信息。结果是,你可以在编写代码时修复它们,而不是在发布到生产环境之后。我们将 Rust 的这一方面昵称为无畏并发(fearless concurrency)。无畏并发允许你编写没有微妙 bug 且易于重构而不会引入新 bug 的代码。

Initially, the Rust team thought that ensuring memory safety and preventing concurrency problems were two separate challenges to be solved with different methods. Over time, the team discovered that the ownership and type systems are a powerful set of tools to help manage memory safety and concurrency problems! By leveraging ownership and type checking, many concurrency errors are compile-time errors in Rust rather than runtime errors. Therefore, rather than making you spend lots of time trying to reproduce the exact circumstances under which a runtime concurrency bug occurs, incorrect code will refuse to compile and present an error explaining the problem. As a result, you can fix your code while you’re working on it rather than potentially after it has been shipped to production. We’ve nicknamed this aspect of Rust fearless concurrency. Fearless concurrency allows you to write code that is free of subtle bugs and is easy to refactor without introducing new bugs.

注意:为了简单起见,我们将许多问题统称为并发,而不是通过说“并发和/或并行”来更精确。在本章中,请在每次我们使用“并发”时,在脑海中将其替换为“并发和/或并行”。在下一章,当这种区别变得更重要时,我们会更加具体。

Note: For simplicity’s sake, we’ll refer to many of the problems as concurrent rather than being more precise by saying concurrent and/or parallel. For this chapter, please mentally substitute concurrent and/or parallel whenever we use concurrent. In the next chapter, where the distinction matters more, we’ll be more specific.

许多语言对处理并发问题所提供的解决方案持有教条式的态度。例如,Erlang 具有优雅的消息传递并发功能,但共享线程间的状态却非常模糊。仅支持可能解决方案的一个子集对于高级语言来说是一种合理的策略,因为高级语言承诺通过放弃一些控制权来换取抽象带来的好处。然而,底层语言被期望在任何给定情况下提供性能最佳的解决方案,并且对硬件的抽象较少。因此,Rust 提供了多种工具,让你能以适合你的情况和要求的方式来对问题建模。

Many languages are dogmatic about the solutions they offer for handling concurrent problems. For example, Erlang has elegant functionality for message-passing concurrency but has only obscure ways to share state between threads. Supporting only a subset of possible solutions is a reasonable strategy for higher-level languages because a higher-level language promises benefits from giving up some control to gain abstractions. However, lower-level languages are expected to provide the solution with the best performance in any given situation and have fewer abstractions over the hardware. Therefore, Rust offers a variety of tools for modeling problems in whatever way is appropriate for your situation and requirements.

以下是我们在本章将涵盖的主题:

Here are the topics we’ll cover in this chapter:

  • 如何创建线程以同时运行多段代码

  • How to create threads to run multiple pieces of code at the same time

  • 消息传递(Message-passing)并发,其中通道在线程间发送消息

  • Message-passing concurrency, where channels send messages between threads

  • 共享状态(Shared-state)并发,其中多个线程有权访问同一块数据

  • Shared-state concurrency, where multiple threads have access to some piece of data

  • SyncSend trait,它们将 Rust 的并发保证扩展到用户定义类型以及标准库提供的类型

  • The Sync and Send traits, which extend Rust’s concurrency guarantees to user-defined types as well as types provided by the standard library