面试题答案
一键面试- 使用
Arc
和Mutex
的原因:Arc
(原子引用计数)用于在多个线程间共享数据,它提供了线程安全的引用计数机制。Mutex
(互斥锁)用于保护共享数据,确保同一时间只有一个线程能访问被其保护的数据。
- 核心代码片段:
use std::sync::{Arc, Mutex};
use std::thread;
// 自定义结构体
struct MyComplexStruct {
// 假设这里有复杂的成员变量
data: String,
}
fn main() {
let shared_hashmap: Arc<Mutex<std::collections::HashMap<String, MyComplexStruct>>> =
Arc::new(Mutex::new(std::collections::HashMap::new()));
let mut handles = vec![];
for _ in 0..10 {
let shared_hashmap_clone = shared_hashmap.clone();
let handle = thread::spawn(move || {
let mut map = shared_hashmap_clone.lock().unwrap();
// 写操作
map.insert("key".to_string(), MyComplexStruct { data: "value".to_string() });
// 读操作
if let Some(value) = map.get("key") {
println!("Read value: {}", value.data);
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}
-
代码逻辑解释:
- 定义自定义结构体:
struct MyComplexStruct
定义了哈希表中存储的复杂数据结构,这里简单地用一个String
类型的data
成员变量表示复杂数据。
- 创建共享哈希表:
let shared_hashmap: Arc<Mutex<std::collections::HashMap<String, MyComplexStruct>>> = Arc::new(Mutex::new(std::collections::HashMap::new()));
创建了一个由Arc
和Mutex
保护的共享哈希表。Arc
使得哈希表可以在多个线程间安全共享,Mutex
确保对哈希表的访问是线程安全的。
- 多线程操作:
- 在
for
循环中,通过let shared_hashmap_clone = shared_hashmap.clone();
克隆Arc
,使得每个线程都有一个指向共享哈希表的引用。 let handle = thread::spawn(move || {... });
创建新线程,并在闭包中进行操作。let mut map = shared_hashmap_clone.lock().unwrap();
获取Mutex
的锁,这里unwrap
用于简单处理锁获取失败的情况(实际应用中可能需要更健壮的错误处理)。一旦获取锁,就可以安全地对哈希表进行读写操作。- 写操作通过
map.insert("key".to_string(), MyComplexStruct { data: "value".to_string() });
向哈希表插入数据。 - 读操作通过
if let Some(value) = map.get("key") {... }
从哈希表读取数据。
- 在
- 等待所有线程完成:
for handle in handles { handle.join().unwrap(); }
等待所有线程执行完毕,确保程序在所有线程完成对共享哈希表的操作后才结束。
- 定义自定义结构体:
-
常函数的应用:
- 虽然在上述代码中没有直接体现常函数,但如果需要对哈希表进行只读操作,可以定义常函数。例如:
fn read_from_hashmap(shared_hashmap: &Arc<Mutex<std::collections::HashMap<String, MyComplexStruct>>>) {
let map = shared_hashmap.lock().unwrap();
if let Some(value) = map.get("key") {
println!("Read value in const function: {}", value.data);
}
}
- 这里
read_from_hashmap
函数接受一个Arc<Mutex<HashMap>>
的引用,通过获取锁后对哈希表进行只读操作,符合常函数的概念,这种函数可以确保在不修改共享数据的情况下进行安全的读取。