pass-by-ref semantics for structs (was Deque impl.)
Dmitry Olshansky
dmitry.olsh at gmail.com
Fri Feb 1 11:36:52 PST 2013
01-Feb-2013 23:10, Rainer Schuetze пишет:
>
>
> On 01.02.2013 09:06, Dmitry Olshansky wrote:
>> 01-Feb-2013 00:32, Rainer Schuetze пишет:
>>>
>>>
>>> - how do you reference count immutable containers? You'll have to cast
>>> the payload to mutable and assume it is not in read-only memory.
>>
>> Containers of immutables can have ref-count easily.
>> And I'd say fully immutable containers are rare case and can rely on GC.
>
> Do you want different implementations of the containers depending on the
> mutable/immutable/shared modifier? Would that be possible?
>
In fact I do ;) It's possible base on element type not the whole
container itself.
>>
>>>
>>> - how do you do reference counting when accessing shared containers in
>>> multiple threads?
>>
>> Atomic Inc/Dec.
>>
>
> Not good enough as shown in the discussion with Steven.
>
>>> 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 :)
--
Dmitry Olshansky
More information about the Digitalmars-d
mailing list