英文:
How to properly manage multithreading read and write access in Rust?
问题
我对 Rust 中的 Arc
和 Mutex
有点困惑。我的问题如下:程序应该使用 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论