如何在Rust中正确管理多线程读写访问?

huangapple go评论105阅读模式
英文:

How to properly manage multithreading read and write access in Rust?

问题

我对 Rust 中的 ArcMutex 有点困惑。我的问题如下:程序应该使用 n+1 个线程,其中只有 1 个线程具有对 data 的写访问权限,而其他 n 个线程只能读取 data。如何在 Rust 中实现这一点?

英文:

I'm kind of confused by Arc and Mutex in rust. My problem is the following:
The program should use n+1 threads, where only 1 thread has write access to data and the other n threads can only read data. How can I achieve this in rust?

答案1

得分: 1

使用 RwLock。使用它,读者不会相互阻塞(但写者仍会阻塞读者,读者也会阻塞写者)。

英文:

Use RwLock. With it, readers don't block each other (but writers still block readers and readers block writers).

huangapple
  • 本文由 发表于 2023年2月14日 19:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定