官术网_书友最值得收藏!

Sharing data

Other than sending data into threads one way, many programs operate on a shared state where multiple execution streams have to access and change one or more shared variables. Typically, this warrants a mutex (short for mutual exclusion), so that any time something is accessed within this locked mutex, it is guaranteed to be a single thread.

This is an old concept and implemented in the Rust standard library. How does that facilitate accessing a variable? Wrapping a variable into a Mutex type will provide for the locking mechanism, thereby making it accessible from multiple concurrent writers. However, they don't have ownership of that memory area yet.

In order to provide that ownership across threads—similar to what Rc does within a single thread—Rust provides the concept of an Arc, an atomic reference counter. Using this Mutex on top, it's the thread-safe equivalent of an Rc wrapping a RefCell, a reference counter that wraps a mutable container. To provide an example, this works nicely:

use std::thread;
use std::sync::{Mutex, Arc};

fn shared_state() {
let v = Arc::new(Mutex::new(vec![]));
let handles = (0..10).map(|i| {
let numbers = Arc::clone(&v);
thread::spawn(move || {
let mut vector = numbers
.lock()
.unwrap();
(*vector).push(i);
})
});

for handle in handles {
handle.join().unwrap();
}
println!("{:?}", *v.lock().unwrap());
}

When running this example, the output is this:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

While the preferred way of doing concurrent programming is still to use immutable variables as often as possible, safe Rust provides the tools for working with shared data without side effects.

主站蜘蛛池模板: 修文县| 奉新县| 江北区| 横峰县| 成都市| 南昌市| 大竹县| 湖北省| 建阳市| 西城区| 中西区| 苍山县| 科技| 平舆县| 阿拉尔市| 自贡市| 松江区| 丰宁| 拉孜县| 万盛区| 乌兰察布市| 板桥市| 安化县| 铁岭县| 汉沽区| 石首市| 阜城县| 弥渡县| 平度市| 丹阳市| 台南县| 巴塘县| 安康市| 巴塘县| 客服| 桐乡市| 台中市| 台山市| 闽清县| 南岸区| 敦化市|