[rust-dev] Reading shared immutable data
mneumann at ntecs.de
Mon Dec 17 18:27:45 PST 2012
Am 18.12.2012 03:11, schrieb Patrick Walton:
> 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.
I will look into this.
> * Use unsafe code by casting to an unsafe pointer and sending the
> unsafe pointer over a channel; do this only as a last resort.
But don't I get into problems with GC when I do that? So I create the
data structure heap allocated in one thread, then get an unsafe pointer
to it and sent
this to all other threads. Of course in the originating thread I need to
keep the reference to the data, otherwise the unsafe pointer will point
> * 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.
But this doesn't allow for parallelism, and that's what we want.
>> 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.
I would just send the data once at thread creation. Hm, I have to play
Thanks a lot!
More information about the Rust-dev