大佬帮忙Go 语言并发读写代码的用Rust 实现?

新手上路,请多包涵

请大佬帮忙把一段之前golang实现的并发读写代码用rust实现

package main

import (
    "fmt"
    "sync"
    "time"
)

type Rule struct {
    Id    int64
    Words []string
}

type Repo struct {
    Version int64
    rwLock  sync.RWMutex
    Rules   []*Rule
}

func main() {
    r0 := Rule{
        Id:    0,
        Words: []string{"a", "b", "c"},
    }
    r1 := Rule{
        Id:    1,
        Words: []string{"0", "1", "2"},
    }
    repo := Repo{
        Version: 0,
        Rules: []*Rule{
            &r0,
            &r1,
        },
    }

    var wg sync.WaitGroup
    wg.Add(2)

    go func() {
        repo.rwLock.RLock()
        rs := repo.Rules
        repo.rwLock.RUnlock()
        time.Sleep(2 * time.Second)
        for i := range rs {
            fmt.Println(rs[i].Id)
        }
        wg.Done()
    }()

    go func() {
        time.Sleep(1 * time.Second)
        repo.rwLock.Lock()
        repo.Rules[1].Id = 999
        repo.Rules = repo.Rules[0:1]
        repo.Rules[0].Id = 1000
        repo.rwLock.Unlock()
        wg.Done()
    }()

    wg.Wait()
}
阅读 733
1 个回答
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

struct Rule {
    id: i64,
    words: Vec<String>,
}

struct Repo {
    version: i64,
    rules: Vec<Arc<Mutex<Rule>>>,
}

fn main() {
    let r0 = Arc::new(Mutex::new(Rule {
        id: 0,
        words: vec!["a".to_string(), "b".to_string(), "c".to_string()],
    }));
    let r1 = Arc::new(Mutex::new(Rule {
        id: 1,
        words: vec!["0".to_string(), "1".to_string(), "2".to_string()],
    }));

    let repo = Arc::new(Repo {
        version: 0,
        rules: vec![r0, r1],
    });

    let mut handles = Vec::new();
    let repo_for_read = Arc::clone(&repo);

    let handle1 = thread::spawn(move || {
        let repo = repo_for_read;
        let rs = repo.rules.clone();
        thread::sleep(Duration::from_secs(2));
        for r in rs.iter() {
            println!("{}", r.lock().unwrap().id);
        }
    });
    handles.push(handle1);

    let repo_for_write = Arc::clone(&repo);
    let handle2 = thread::spawn(move || {
        thread::sleep(Duration::from_secs(1));
        let mut rules = repo_for_write.rules.clone();
        if let Some(r) = rules.get_mut(1) {
            let mut r = r.lock().unwrap();
            r.id = 999;
        }
        rules.remove(1);
        if let Some(r) = rules.get_mut(0) {
            let mut r = r.lock().unwrap();
            r.id = 1000;
        }
    });
    handles.push(handle2);

    for handle in handles {
        handle.join().unwrap();
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏