最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mutex - Rust syntax nested struct with reference - Stack Overflow

programmeradmin1浏览0评论

There is a large array arr of structures A containing references to nested structures B. There is one mutex allowing write access to the array arr. How to describe references to B so that if there is a write reference to arr, the thread would have write access to the nested structures of B as well?

There is an option:

struct A {
    b_ref: Arc<Mutex<B>>    // Reference to B
}
struct MyArray {
    arr: Vec<A>,    // Array of structures A
    mutex: Mutex<()>    // Shared mutex for accessing array arr
}

But with a large arr, having many mutexes is inefficient

There is also an option here: Nested struct with reference But it uses unsafe

发布评论

评论列表(0)

  1. 暂无评论