x-i18n: generated_at: “2026-03-01T14:37:37Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 0101c582d4410d0136c4ef46195c3fa382438dc1a667cbe5d754605e2aa89fb2 source_path: ch16-00-concurrency.md workflow: 16
无畏并发 (Fearless Concurrency)
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 中的许多并发错误都是编译时错误,而不是运行时错误。因此,与其让你花费大量时间试图重现运行时并发错误发生的确切情况,不如让错误的代码拒绝编译并显示解释问题的错误。结果,你可以在编写代码时修复它,而不是可能在代码发布到生产环境后才修复。我们将 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.
注意:为了简单起见,我们将许多问题称为“并发 (concurrent)”,而不是更精确地称其为“并发和/或并行 (concurrent and/or parallel)”。在本章中,每当我们使用“并发”时,请在脑海中将其替换为“并发和/或并行”。在下一章中,这种区别更为重要,我们将更加具体。
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.
以下是我们在本章中将要介绍的主题:
- 如何创建线程以同时运行多段代码
- “消息传递 (Message-passing)”并发,其中通道 (channels) 在线程之间发送消息
- “共享状态 (Shared-state)”并发,其中多个线程可以访问某段数据
Sync和Send特征,它们将 Rust 的并发保证扩展到用户定义的类型以及标准库提供的类型
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 concurrency, where channels send messages between threads
- Shared-state concurrency, where multiple threads have access to some piece of data
- The
SyncandSendtraits, which extend Rust’s concurrency guarantees to user-defined types as well as types provided by the standard library