Shared

Simen Kjærås simen.kjaras at gmail.com
Thu May 16 17:11:19 UTC 2019


On Thursday, 16 May 2019 at 15:47:37 UTC, Dominikus Dittes 
Scherkl wrote:
> On Thursday, 16 May 2019 at 11:15:13 UTC, Jonathan M Davis 
> wrote:
>> [...] that is assuming that it's not possible to have any 
>> other references to the same data, which would be too 
>> restrictive to be useful. Having multiple references to the 
>> same data is a core concept of sharing data across threads.
> Hm. I don't understand the necessity of this. If I have a 
> shared symbol x,
> this same symbol can be used in different threads, no? I can't 
> see why multiple threads need to have different references to 
> the same data. They just can use the same reference.

Well, no. The reference is the part that gets copied around. It's 
the address of the shared object. When you do 'auto ref2 = 
ref1;', you're creating a new reference to an already existing 
object, but leaving the object itself unchanged.

If more than one thread can access the same object, that means 
they each have a copy of the reference*. In the same way, when 
you call a function with a class argument, that function receives 
a copy of the reference (but not the object). Clearly then, the 
ability to create multiple references to the same object must be 
possible without breaking shared.

--
   Simen

* barring __gshared tricks


More information about the Digitalmars-d mailing list