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

相关推荐
s9123601011 天前
【Rust】时间轮的数据结构于设计模式
rust
盒马盒马1 天前
Rust:Trait 抽象接口 & 特征约束
开发语言·rust
I still …1 天前
Rust内存问题检测
rust·程序分析·漏洞检测
百锦再1 天前
大型省级政务平台采用金仓数据库(KingbaseES)
开发语言·数据库·后端·rust·eclipse
受之以蒙1 天前
赋能 AI 与具身智能:Rust ndarray 构建安全高效的数据底座
人工智能·笔记·rust
Pomelo_刘金1 天前
Rust : 新版本 1.90.0
rust
songroom1 天前
Rust: 量化策略回测与简易线程池构建、子线程执行观测
开发语言·后端·rust
资深web全栈开发1 天前
二分搜索中 `right = mid` 而非 `right = mid + 1` 的解释
算法·rust·二分搜索
Pomelo_刘金1 天前
Rust : 新版本 1.89.0
rust
Pomelo_刘金1 天前
Rust : Trusted Publishing(受信发布)
rust