[rust-dev] Reading shared immutable data
pwalton at mozilla.com
Mon Dec 17 18:11:16 PST 2012
On 12/17/12 6:03 PM, Michael Neumann wrote:
> We have a very huge immutable data structure that we want to share
> (read-only) between many light-weight threads.
> From what I have seen, I should use Arc. Is there any other way to
> share the data between threads?
You can also:
* Use a reader-writer lock (RWLock). This is still safe.
* Use unsafe code by casting to an unsafe pointer and sending the unsafe
pointer over a channel; do this only as a last resort.
* Turn your shared state into a task and have other tasks access the
data by sending it messages. This is the classical actor model solution,
but usually ARC will be more efficient.
> And when using Arc, can I access the data in parallel by all threads?
Yes. The only synchronization cost you'll pay is the cost of an atomic
CPU instruction whenever you send the data.
More information about the Rust-dev