- Hands-On Data Structures and Algorithms with Rust
- Claus Matzinger
- 206字
- 2021-07-02 14:11:42
Concurrency and mutability
Rust's approach to managing memory is a powerful concept. In fact, it is powerful enough to also facilitate concurrency and parallel execution. However, first things first: how do threads work in the Rust standard library?
Concurrency and parallelism are two different modes of execution. While concurrency means that parts of a program run independently of each other, parallelism refers to these parts executing at the same time. For simplicity, we will refer to both concepts as concurrency.
Due to its low-level nature, Rust provides an API to the operating system's threading capabilities (for example, POSIX on Linux/Unix systems). If no variables are passed into the scope, their usage is very straightforward:
use std::thread;
fn threading() {
// The to pipes (||) is the space where parameters go,
// akin to a function signature's parameters, without
// the need to always declare types explicitly.
// This way, variables can move from the outer into the inner scope
let handle = thread::spawn(|| {
println!("Hello from a thread");
});
handle.join().unwrap();
}
However, when passing data back and forth, more work has to be done to hold up Rust's safety guarantees, especially when mutability comes into play. Before getting into that, it is important to recap immutability.
推薦閱讀
- Unity 5.x Game AI Programming Cookbook
- 復雜性思考:復雜性科學和計算模型(原書第2版)
- Python金融大數據分析(第2版)
- 大數據可視化
- Modern Programming: Object Oriented Programming and Best Practices
- Oracle RAC 11g實戰指南
- Hadoop與大數據挖掘(第2版)
- Learn Unity ML-Agents:Fundamentals of Unity Machine Learning
- Ceph源碼分析
- 軟件成本度量國家標準實施指南:理論、方法與實踐
- 城市計算
- 智能數據分析:入門、實戰與平臺構建
- Remote Usability Testing
- Spark大數據編程實用教程
- 數字媒體交互設計(初級):Web產品交互設計方法與案例