Rust - 消息传递

Rust 主要通过通道(Channel)进行消息传递,它基于"共享内存不如传递消息"的理念。Rust 中消息传递的实现方式主要有以下几种:

通道(Channel)

通道是 Rust 标准库提供的消息传递机制,分为同步通道和异步通道。其特点是发送者和接收者可以位于不同线程,实现线程间安全通信。 下面是一个使用同步通道的示例:

rust 复制代码
use std::sync::mpsc; 
use std::thread; 
fn main() { 
    // 创建一个同步通道,返回发送端和接收端 
    let (tx, rx) = mpsc::channel(); 
    // 启动一个新线程 
    thread::spawn(move || { 
        // 发送消息到通道 
        tx.send("Hello from thread!").unwrap(); 
        println!("Message sent"); 
    }); 
    // 主线程接收消息 
    let received = rx.recv().unwrap(); 
    println!("Received: {}", received); 
} 

多生产者单消费者(mpsc)

Rust 标准库提供的 mpsc(Multiple Producers, Single Consumer)通道允许多个发送者向同一个接收者发送消息。 下面是一个多生产者的示例:

rust 复制代码
use std::sync::mpsc; 
use std::thread;
use std::time::Duration; 
fn main() { 
    let (tx, rx) = mpsc::channel(); 
    let tx1 = tx.clone(); // 克隆发送端以创建多个生产者 
    
    // 第一个线程发送消息 
    thread::spawn(move || { 
        tx.send("Message 1").unwrap(); 
        thread::sleep(Duration::from_secs(1)); 
    }); 
    
    // 第二个线程发送消息 
    thread::spawn(move || { 
        tx1.send("Message 2").unwrap();
        thread::sleep(Duration::from_secs(1)); 
    }); 
    
    // 主线程接收消息 
    for received in rx { 
        println!("Got: {}", received); 
        } 
} 

异步通道

在异步编程中,可以使用 tokioasync-std 提供的异步通道。 下面是一个使用 tokio 异步通道的示例:

rust 复制代码
use tokio::sync::mpsc; 
use tokio::time::{sleep, Duration};

#[tokio::main] 
async fn main() { 
    // 创建一个容量为32的异步通道 
    let (tx, mut rx) = mpsc::channel(32); 
    // 启动一个异步任务发送消息 
    tokio::spawn(async move { 
        tx.send("Hello from async task!").await.unwrap(); 
        println!("Async message sent"); 
    }); 
    
    // 主线程接收消息 
    if let Some(msg) = rx.recv().await { 
        println!("Received: {}", msg); 
    } 
} 

所有权转移

Rust 的消息传递涉及所有权转移,这确保了线程安全。当一个值被发送到通道时,其所有权也随之转移,避免了数据竞争。

rust 复制代码
use std::sync::mpsc; 
use std::thread; 
fn main() { 
    let (tx, rx) = mpsc::channel(); 
    let data = vec![1, 2, 3]; 
    // 转移 data 的所有权到新线程 
    thread::spawn(move || { 
        tx.send(data).unwrap(); 
        // 这里无法再使用 data,因为所有权已转移 
    }); 
    // 主线程接收数据并获得所有权 
    let received_data = rx.recv().unwrap(); 
    println!("Received data: {:?}", received_data); 
} 

总结

Rust 的消息传递机制依靠通道实现线程间通信,通过所有权转移保证内存安全,是构建并发系统的重要基础。我们可以根据需求选择同步通道、异步通道或其他第三方库提供的消息传递方式。

相关推荐
techdashen3 小时前
Cloudflare 为何抛弃 NGINX,用 Rust 自研了一个代理
运维·nginx·rust
Mr.Rice.Fool5 小时前
rust面试经验1
后端·面试·职场和发展·rust
本地化文档7 小时前
rust-nomicon-l10n
rust·github·gitcode
代码羊羊7 小时前
Rust 格式化输出完全攻略:从入门到精通
开发语言·后端·rust
Rust研习社7 小时前
Rust + PostgreSQL 极简技术栈应用开发
开发语言·数据库·后端·http·postgresql·rust
古城小栈8 小时前
rust 亿级并发模型,实践完成
开发语言·网络·rust
qcx239 小时前
Warp源码深度解析(一):GPU加速+AI Agent的下一代终端架构全景
人工智能·架构·rust
暮色念了红尘9 小时前
CC Switch — Ubuntu 20.04 可用版本
ubuntu·ai·rust·ubuntu 20.04·vibe coding·cc swich
qcx239 小时前
Warp源码深度解析(二):自研GPU UI框架——WarpUI的ECH模式与渲染管线
人工智能·ui·设计模式·rust