pass-by-ref semantics for structs (was Deque impl.)
Rainer Schuetze
r.sagitario at gmx.de
Thu Jan 31 12:55:11 PST 2013
On 31.01.2013 21:48, Steven Schveighoffer wrote:
> On Thu, 31 Jan 2013 15:32:23 -0500, Rainer Schuetze <r.sagitario at gmx.de>
> wrote:
>
>> - 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.
>
> The reference count part is not immutable.
>
> e.g.:
>
> struct refcountstruct
> {
> int count;
> immutable(containerimpl) impl;
> }
What about immutable(refcountstruct)? Maybe only implicitely as field of
an immutable class.
>
>>
>> - how do you do reference counting when accessing shared containers in
>> multiple threads? 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.
>
> I would be concerned if a thread can get ahold of a reference counted
> pointer without having the counter incremented already on its behalf.
>
You have to read the pointer to increment the counter to get hold of the
reference.
More information about the Digitalmars-d
mailing list