前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Rust工具集之arc-swap

Rust工具集之arc-swap

作者头像
newbmiao
发布2024-06-14 14:23:14
680
发布2024-06-14 14:23:14
举报
文章被收录于专栏:学点Rust学点Rust

Rust中常有配置读写的操作,一般可以用Arc<RwLock<T>>来实现,也可以用arc-swap来。有什么不同?

下面拿一个例子来说明下

假设有个配置,会有一个 read thread 不断获取配置,配置更新时要实时反映出来。

文章目录

  • RwLock
  • arc-swap

RwLock

RwLock实现一下

代码语言:javascript
复制
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;

fn main() {
    let config = Arc::new(RwLock::new("Initial Config".to_string()));
    let shared_config = Arc::clone(&config);

    let reader_thread = {
        thread::spawn(move || loop {
            // 1. read config
            let current_config = shared_config.read().unwrap();
            println!("Reader thread: {}", *current_config);
            thread::sleep(Duration::from_secs(2));
        })
    };

    // Update the configuration after 1s
    thread::sleep(Duration::from_secs(1));

    println!("Updating config");
    // 2. write config
    let mut current_config = config.write().unwrap();
    *current_config = "Updated Config".to_string();
    println!("Updating config done");
    reader_thread.join().unwrap();
}

但如果你执行会发现,2 修改配置后,1 读取配置还是没有变化,而且不会打印Updating config done, 为啥呢?

这里其实是一个错误示范,RwLock读会持有锁,如果不释放锁,可以加多个读锁,但是不能获取写锁来修改。

修改代码,用作用域来释放锁

代码语言:javascript
复制
// 1. read config
{
    let current_config = shared_config.read().unwrap();
    println!("Reader thread: {}", *current_config);
}

...
// 2. write config
{
    let mut current_config = config.write().unwrap();
    *current_config = "Updated Config".to_string();
    println!("Updating config done");
}

现在结果就满足预期了:

代码语言:javascript
复制
Reader thread: Initial Config
Updating config
Updating config done
Reader thread: Updated Config
...

arc-swap

这样使用RwLock修改配置需要注意锁的持有和释放,本质上是无法原子操作读写,导致代码实现上不够友好,也容易写出死锁的代码。

arc-swap主要就是为了优化这点,原子操作资源,让你可以直接编写读写操作而无需顾虑锁。

代码语言:javascript
复制
use arc_swap::ArcSwap;
use std::sync::Arc;
use std::thread;
use std::time::Duration;

fn main() {
    let config = Arc::new("Initial Config".to_string());
    let shared_config = Arc::new(ArcSwap::new(config));

    let reader_thread = {
        let shared_config = Arc::clone(&shared_config);
        thread::spawn(move || loop {
            let current_config = shared_config.load();
            println!("Reader thread: {}", current_config);
            thread::sleep(Duration::from_secs(1));
        })
    };

    thread::sleep(Duration::from_secs(2));

    let new_config = Arc::new("Updated Config".to_string());
    shared_config.store(new_config);

    reader_thread.join().unwrap();
}

怎么样,代码是不是直截了当了许多?

一般很多读多写少的场景都可以试试arc-swap

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 菜鸟Miao 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • RwLock
  • arc-swap
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档