Creating immutable data and sharing it
Lutger
lutger.blijdestijn at gmail.com
Tue Sep 21 05:17:34 PDT 2010
Simen kjaeraas wrote:
> Lutger <lutger.blijdestijn at gmail.com> wrote:
>
>> char[] s = ...;
>> immutable(char)[] p = cast(immutable)s.dup; // ok, unique reference
>>
>> I do not understand how that works with sharing. Since immutable data is
>> implicitly shared but the data that p refers to is allocated on the TLS,
>> how can
>> you share this? I always thought that threads do not have access to each
>> others
>> TLS at all?
>
> Only p itself is in TLS - the pointed-to data is on the heap.
Aha, thanks. I have made a conceptual diagram to help understand this, would you
care to take a look and confirm whether this is correct or not?
http://picasaweb.google.com/Lutger.blijdestijn/Illustration#5519337139518828386
I hope it explains itself, the edges reflect the types and the nodes the memory
storage.
>
>> Finally a practical question: when you have a data structure that is too
>> complex
>> to create in a constructor, want to create it and then make it
>> immutable, what
>> is the current way to go about this? If it is created by one thread,
>> would it be
>> ok to type it as __gshared, cast to immutable and then send a message to
>> other
>> threads? Is __gshared required in this case?
>
> Immutable global state may be instantiated from non-immutable data in
> module constructors. I believe that is the canonical way.
>
Sometimes this is not possible. For example if you want to create a data
structure from user input once, then use it read-only for the rest of the
program.
More information about the Digitalmars-d-learn
mailing list