欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Rust 中的 Tokio 線程同步機制詳解

 更新時間:2025年09月23日 09:03:48   作者:天翼云開發(fā)者社區(qū)  
Rust中的Tokio提供了多種線程同步機制,包括互斥鎖、讀寫鎖、屏障、信號量、通知及消息通道,用于協(xié)調(diào)并發(fā)訪問、資源共享和狀態(tài)同步,提升程序的安全性和效率,本文給大家介紹Rust中的Tokio線程同步機制,感興趣的朋友一起看看吧

Rust 中的 Tokio 線程同步機制

在并發(fā)編程中,線程同步是一個重要的概念,用于確保多個線程在訪問共享資源時能夠正確地協(xié)調(diào)。Tokio 是一個強大的異步運行時庫,為 Rust 提供了多種線程同步機制。以下是一些常見的同步機制:

  • Mutex
  • RwLock
  • Barrier
  • Semaphore
  • Notify
  • oneshot 和 mpsc 通道
  • watch 通道

1. Mutex

Mutex(互斥鎖)是最常見的同步原語之一,用于保護共享數(shù)據(jù)。它確保同一時間只有一個線程能夠訪問數(shù)據(jù),從而避免競爭條件。

use tokio::sync::Mutex;
use std::sync::Arc;
?
#[tokio::main]
async fn main() {
    let data = Arc::new(Mutex::new(0));
?
    let mut handles = vec![];
    for _ in 0..10 {
        let data = data.clone();
        let handle = tokio::spawn(async move {
            let mut lock = data.lock().await;
            *lock += 1;
        });
        handles.push(handle);
    }
?
    for handle in handles {
        handle.await.unwrap();
    }
?
    println!("Result: {}", *data.lock().await);
}

2. RwLock

RwLock(讀寫鎖)允許多線程同時讀取數(shù)據(jù),但只允許一個線程寫入數(shù)據(jù)。它比 Mutex 更加靈活,因為在讀取多于寫入的場景下,它能提高性能。功能上,他是讀寫互斥、寫寫互斥、讀讀兼容。

use tokio::sync::RwLock;
use std::sync::Arc;
?
#[tokio::main]
async fn main() {
    let data = Arc::new(RwLock::new(0));
?
    let read_data = data.clone();
    let read_handle = tokio::spawn(async move {
        let lock = read_data.read().await;
        println!("Read: {}", *lock);
    });
?
    let write_data = data.clone();
    let write_handle = tokio::spawn(async move {
        let mut lock = write_data.write().await;
        *lock += 1;
        println!("Write: {}", *lock);
    });
?
    read_handle.await.unwrap();
    write_handle.await.unwrap();
}

3. Barrier

Barrier 是一種同步機制,允許多個線程在某個點上進行同步。當線程到達屏障時,它們會等待直到所有線程都到達,然后一起繼續(xù)執(zhí)行。

use tokio::sync::Barrier;
use std::sync::Arc;
?
#[tokio::main]
async fn main() {
    let barrier = Arc::new(Barrier::new(3));
?
    let mut handles = vec![];
    for i in 0..3 {
        let barrier = barrier.clone();
        let handle = tokio::spawn(async move {
            println!("Before wait: {}", i);
            barrier.wait().await;
            println!("After wait: {}", i);
        });
        handles.push(handle);
    }
?
    for handle in handles {
        handle.await.unwrap();
    }
}

4. Semaphore

Semaphore(信號量)是一種用于控制對資源訪問的同步原語。它允許多個線程訪問資源,但有一個最大并發(fā)數(shù)限制。

#[tokio::test]
async fn test_sem() {
    let semaphore = Arc::new(Semaphore::new(3));
?
    let mut handles = vec![];
    for i in 0..5 {
        let semaphore = semaphore.clone();
        let handle = tokio::spawn(async move {
            let permit = semaphore.acquire().await.unwrap();
            let now = Local::now();
            println!("Got permit: {} at {:?}", i, now);
            println!(
                "Semaphore available permits before sleep: {}",
                semaphore.available_permits()
            );
            sleep(Duration::from_secs(5)).await;
            drop(permit);
            println!(
                "Semaphore available permits after sleep: {}",
                semaphore.available_permits()
            );
        });
        handles.push(handle);
    }
?
    for handle in handles {
        handle.await.unwrap();
    }
}

最終的結果如下

Got permit: 0 at 2024-08-08T21:03:04.374666+08:00
Semaphore available permits before sleep: 2
Got permit: 1 at 2024-08-08T21:03:04.375527800+08:00
Semaphore available permits before sleep: 1
Got permit: 2 at 2024-08-08T21:03:04.375563+08:00
Semaphore available permits before sleep: 0
Semaphore available permits after sleep: 0
Semaphore available permits after sleep: 0
Semaphore available permits after sleep: 1
Got permit: 3 at 2024-08-08T21:03:09.376722800+08:00
Semaphore available permits before sleep: 1
Got permit: 4 at 2024-08-08T21:03:09.376779200+08:00
Semaphore available permits before sleep: 1
Semaphore available permits after sleep: 2
Semaphore available permits after sleep: 3

5. Notify

Notify 是一種用于線程間通知的簡單機制。它允許一個線程通知其他線程某些事件的發(fā)生。

use tokio::sync::Notify;
use std::sync::Arc;
?
#[tokio::main]
async fn main() {
    let notify = Arc::new(Notify::new());
    let notify_clone = notify.clone();
?
    let handle = tokio::spawn(async move {
        notify_clone.notified().await;
        println!("Received notification");
    });
?
    notify.notify_one();
    handle.await.unwrap();
}

6. oneshot 和 mpsc 通道

oneshot 通道用于一次性發(fā)送消息,而 mpsc 通道則允許多個生產(chǎn)者發(fā)送消息到一個消費者。一般地onshot用于異常通知、啟動分析等功能。mpsc用于實現(xiàn)異步消息同步

oneshot

use tokio::sync::oneshot;
?
#[tokio::main]
async fn main() {
    let (tx, rx) = oneshot::channel();
?
    tokio::spawn(async move {
        tx.send("Hello, world!").unwrap();
    });
?
    let message = rx.await.unwrap();
    println!("Received: {}", message);
}

mpsc

use tokio::sync::mpsc;
?
#[tokio::main]
async fn main() {
    let (tx, mut rx) = mpsc::channel(32);
?
    tokio::spawn(async move {
        tx.send("Hello, world!").await.unwrap();
    });
?
    while let Some(message) = rx.recv().await {
        println!("Received: {}", message);
    }
}

7. watch 通道

watch 通道用于發(fā)送和接收共享狀態(tài)的更新。它允許多個消費者監(jiān)聽狀態(tài)的變化。

use tokio::sync::watch;
?
#[tokio::main]
async fn main() {
    let (tx, mut rx) = watch::channel("initial");
?
    tokio::spawn(async move {
        tx.send("updated").unwrap();
    });
?
    while rx.changed().await.is_ok() {
        println!("Received: {}", *rx.borrow());
    }
}

?watch通道?:

  • 用于廣播狀態(tài)更新,一個生產(chǎn)者更新狀態(tài),多個消費者獲取最新狀態(tài)。
  • 適合配置變更、狀態(tài)同步等場景。

?mpsc通道?:

  • 用于傳遞消息隊列,多個生產(chǎn)者發(fā)送消息,一個消費者逐條處理。
  • 適合任務隊列、事件驅(qū)動等場景。

總結

Rust 中的 Tokio 提供了豐富的線程同步機制,可以根據(jù)具體需求選擇合適的同步原語。常用的同步機制包括:

  • Mutex:互斥鎖,保護共享數(shù)據(jù)。
  • RwLock:讀寫鎖,允許并發(fā)讀,寫時獨占。
  • Barrier:屏障,同步多個線程在某一點。
  • Semaphore:信號量,控制并發(fā)訪問資源。
  • Notify:通知機制,用于線程間通知。
  • oneshot 和 mpsc 通道:消息傳遞機制。
  • watch 通道:狀態(tài)更新機制。

通過這些同步機制,可以在 Rust 中編寫高效、安全的并發(fā)程序。

到此這篇關于Rust 中的 Tokio 線程同步機制的文章就介紹到這了,更多相關rust tokio線程同步內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 使用Rust開發(fā)小游戲完成過程

    使用Rust開發(fā)小游戲完成過程

    這篇文章主要介紹了使用Rust開發(fā)小游戲的完整過程,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • Rust文件 launch.json作用大全

    Rust文件 launch.json作用大全

    launch.json 是 Visual Studio Code(VSCode)中的一個配置文件,主要用于配置調(diào)試器,本文給大家介紹Rust文件 launch.json 有什么用,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • rust閉包的使用

    rust閉包的使用

    閉包在Rust中是非常強大的功能,允許你編寫更靈活和表達性的代碼,本文主要介紹了rust閉包的使用,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • Rust讀取配置文件的實現(xiàn)

    Rust讀取配置文件的實現(xiàn)

    本文主要介紹了Rust讀取配置文件的實現(xiàn),主要讀取Cargo.toml文件,讀取.env文件和讀取自定義toml文件這三種,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Rust包和Crate超詳細講解

    Rust包和Crate超詳細講解

    這篇文章主要介紹了Rust包管理和Crate,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-12-12
  • Rust使用kind進行異常處理(錯誤的分類與傳遞)

    Rust使用kind進行異常處理(錯誤的分類與傳遞)

    Rust?有一套獨特的處理異常情況的機制,它并不像其它語言中的?try?機制那樣簡單,這篇文章主要介紹了Rust指南錯誤的分類與傳遞以及使用kind進行異常處理,需要的朋友可以參考下
    2022-09-09
  • Rust的泛型、Traits與生命周期用法及說明

    Rust的泛型、Traits與生命周期用法及說明

    本文通過一個尋找列表中最大值的示例,展示了如何從重復代碼中提取函數(shù),再利用泛型實現(xiàn)代碼復用,主要步驟包括:識別重復邏輯;抽象提??;泛型應用;進一步擴展,通過不斷抽象和泛化,我們不僅能減少代碼重復,還能寫出更通用、健壯和可維護的代碼
    2025-02-02
  • Rust中的模塊系統(tǒng)之控制作用域與私有性詳解

    Rust中的模塊系統(tǒng)之控制作用域與私有性詳解

    這篇文章總結了Rust模塊系統(tǒng)的基本規(guī)則,包括如何聲明模塊、路徑訪問、私有性與公開性,以及如何使用`use`關鍵字簡化路徑引用,通過一個餐廳系統(tǒng)示例,展示了如何利用模塊劃分功能,并介紹了如何在其他模塊或二進制crate中使用這些模塊
    2025-02-02
  • 最新Rust錯誤處理簡介

    最新Rust錯誤處理簡介

    Rust并不像C++一樣使用try?catch的異常機制來進行錯誤處理,他將錯誤分為可恢復錯誤和不可恢復錯誤兩類,主要使用panic!宏和Result<T,E>類型來進行錯誤處理,這篇文章主要介紹了Rust錯誤處理簡介,需要的朋友可以參考下
    2022-11-11
  • rust多個mod文件引用和文件夾mod使用注意事項小結

    rust多個mod文件引用和文件夾mod使用注意事項小結

    在 Rust 項目中,可以使用 mod 關鍵字將一個文件夾或一個 rs 文件作為一個模塊引入到當前文件中,本文給大家介紹rust多個mod文件引用和文件夾mod使用注意事項小結,感興趣的朋友跟隨小編一起看看吧
    2024-03-03

最新評論