pass-by-ref semantics for structs (was Deque impl.)

Rainer Schuetze r.sagitario at gmx.de
Fri Feb 1 12:17:14 PST 2013


On 01.02.2013 20:36, Dmitry Olshansky wrote:
> 01-Feb-2013 23:10, Rainer Schuetze пишет:
>>>> Consider clearing the last reference to a shared
>>>> reference counted object while another thread reads this reference.
>>>> With
>>>> usual atomic operations on the reference count only:
>>>>
>>>>    1. the reading thread reads the payload pointer
>>>>    2. the writing thread reads the payload pointer, decrements the
>>>> reference count and frees the array
>>>>    3. the reading thread increments the reference count, but accesses
>>>> the deallocated array.
>>>
>>> Can't happen as reading thread got to have a reference and so does the
>>> writing thread, then refcount >= 2.
>>
>> Can happen with a single shared reference accessed from two threads.
>
> You mean shared reference to shared ref-counter object?
> Awful and BTW impossible in D as you'd have to cast your way into it :)
>

Maybe I'm coding too much C++, but isn't this what the "shared" modifier 
is all about?


More information about the Digitalmars-d mailing list