Rust并发安全模式:从线程同步到无锁编程
引言
并发安全是Rust最引人注目的特性之一。作为从Python转向Rust的后端开发者,我深刻体会到Rust在编译时保证线程安全的独特优势。Rust的所有权系统消除了数据竞争的可能性,让并发编程变得更加安全和高效。本文将深入探讨Rust的并发安全模式,帮助你编写可靠的并发代码。
一、并发安全基础
1.1 数据竞争问题
数据竞争发生在以下条件同时满足时:
- 多个线程访问同一数据
- 至少有一个线程写入数据
- 没有足够的同步机制
1.2 Rust的解决方案
Rust通过所有权系统在编译时防止数据竞争:
- 所有权规则:每个值只能有一个所有者
- 借用规则:可变引用和不可变引用不能同时存在
- 生命周期:确保引用的有效性
二、线程同步原语
2.1 Mutex
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
2.2 RwLock
use std::sync::{Arc, RwLock};
use std::thread;
fn main() {
let data = Arc::new(RwLock::new(vec![1, 2, 3]));
// 多个读操作可以同时进行
for i in 0..3 {
let data = Arc::clone(&data);
thread::spawn(move || {
let read = data.read().unwrap();
println!("Reader {}: {:?}", i, *read);
});
}
// 写操作独占访问
let data = Arc::clone(&data);
thread::spawn(move || {
let mut write = data.write().unwrap();
write.push(4);
println!("Writer: {:?}", *write);
}).join().unwrap();
}
2.3 Condvar
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
fn main() {
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = Arc::clone(&pair);
thread::spawn(move || {
let (lock, cvar) = &*pair2;
let mut started = lock.lock().unwrap();
*started = true;
cvar.notify_one();
});
let (lock, cvar) = &*pair;
let mut started = lock.lock().unwrap();
while !*started {
started = cvar.wait(started).unwrap();
}
println!("Thread started");
}
三、消息传递
3.1 Channel
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
let received = rx.recv().unwrap();
println!("Received: {}", received);
}
3.2 多生产者
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
for i in 0..3 {
let tx = tx.clone();
thread::spawn(move || {
let message = format!("Message {}", i);
tx.send(message).unwrap();
});
}
for received in rx {
println!("Received: {}", received);
}
}
3.3 crossbeam-channel
use crossbeam_channel as channel;
use std::thread;
fn main() {
let (snd, rcv) = channel::unbounded();
thread::spawn(move || {
snd.send(42).unwrap();
});
println!("Received: {}", rcv.recv().unwrap());
}
四、无锁编程
4.1 Atomic类型
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
fn main() {
let counter = AtomicUsize::new(0);
let mut handles = vec![];
for _ in 0..10 {
let handle = thread::spawn(move || {
counter.fetch_add(1, Ordering::SeqCst);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", counter.load(Ordering::SeqCst));
}
4.2 原子引用计数
use std::sync::Arc;
use std::thread;
fn main() {
let data = Arc::new(vec![1, 2, 3]);
for _ in 0..5 {
let data = Arc::clone(&data);
thread::spawn(move || {
println!("Data: {:?}", data);
});
}
}
4.3 CAS操作
use std::sync::atomic::{AtomicI32, Ordering};
fn compare_and_swap_example() {
let value = AtomicI32::new(5);
// 尝试将5替换为10
let result = value.compare_and_swap(5, 10, Ordering::SeqCst);
assert_eq!(result, 5); // 返回旧值
// 再次尝试(当前值是10,不是5)
let result = value.compare_and_swap(5, 20, Ordering::SeqCst);
assert_eq!(result, 10); // 返回当前值,交换失败
}
五、并发数据结构
5.1 ConcurrentHashMap
use dashmap::DashMap;
use std::thread;
fn main() {
let map = DashMap::new();
let handles: Vec<_> = (0..10).map(|i| {
let map = map.clone();
thread::spawn(move || {
map.insert(i, i * 2);
})
}).collect();
for handle in handles {
handle.join().unwrap();
}
println!("Map contains {} entries", map.len());
}
5.2 ConcurrentQueue
use crossbeam::queue::SegQueue;
use std::thread;
fn main() {
let queue = SegQueue::new();
// 生产者
thread::spawn(move || {
for i in 0..10 {
queue.push(i);
}
});
// 消费者
thread::spawn(move || {
while let Some(val) = queue.pop() {
println!("Got: {}", val);
}
}).join().unwrap();
}
六、实战:并发任务调度器
6.1 任务队列
use std::sync::{Arc, Mutex};
use std::thread;
use std::collections::VecDeque;
struct TaskQueue {
queue: Mutex<VecDeque<Box<dyn FnOnce() + Send>>>,
}
impl TaskQueue {
fn new() -> Self {
TaskQueue {
queue: Mutex::new(VecDeque::new()),
}
}
fn push(&self, task: Box<dyn FnOnce() + Send>) {
self.queue.lock().unwrap().push_back(task);
}
fn pop(&self) -> Option<Box<dyn FnOnce() + Send>> {
self.queue.lock().unwrap().pop_front()
}
}
6.2 Worker线程
struct Worker {
thread: Option<thread::JoinHandle<()>>,
}
impl Worker {
fn new(id: usize, queue: Arc<TaskQueue>) -> Self {
let thread = thread::spawn(move || {
loop {
if let Some(task) = queue.pop() {
println!("Worker {} executing task", id);
task();
}
}
});
Worker {
thread: Some(thread),
}
}
}
七、并发安全最佳实践
7.1 避免共享状态
// 不好:共享可变状态
fn bad_example() {
let mut data = Arc::new(Mutex::new(vec![]));
for _ in 0..10 {
let data = Arc::clone(&data);
thread::spawn(move || {
let mut data = data.lock().unwrap();
data.push(1);
});
}
}
// 好:使用消息传递
fn good_example() {
let (tx, rx) = mpsc::channel();
for _ in 0..10 {
let tx = tx.clone();
thread::spawn(move || {
tx.send(1).unwrap();
});
}
}
7.2 使用线程池
use rayon::prelude::*;
fn process_data(data: Vec<i32>) -> Vec<i32> {
data.par_iter()
.map(|x| x * 2)
.collect()
}
7.3 优雅关闭
use tokio::signal;
#[tokio::main]
async fn main() {
let server = axum::Server::bind(&([127, 0, 0, 1], 3000).into())
.serve(app.into_make_service());
let graceful = server.with_graceful_shutdown(shutdown_signal());
if let Err(e) = graceful.await {
eprintln!("Server error: {}", e);
}
}
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}
八、总结
Rust的并发安全特性通过所有权系统在编译时保证了线程安全,消除了数据竞争的可能性。通过使用线程同步原语、消息传递和无锁编程,我们可以构建高效、安全的并发应用。
关键要点:
- 使用Arc+Mutex:安全地共享可变数据
- 优先使用消息传递:避免共享状态
- 使用原子操作:无锁编程
- 使用线程池:管理线程数量
- 优雅关闭:正确处理系统信号
从Python转向Rust后,我发现Rust的并发编程更加安全和高效,编译时的检查大大减少了运行时错误。
延伸阅读
- Rust官方并发编程指南
- crossbeam库文档
- dashmap库文档
- 《Rust并发编程实战》书籍
3万+



被折叠的 条评论
为什么被折叠?



