Creating immutable data and sharing it

Simen kjaeraas simen.kjaras at gmail.com
Tue Sep 21 03:37:25 PDT 2010


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.


> 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.

-- 
Simen


More information about the Digitalmars-d-learn mailing list