On Thu, 31 Jan 2013 15:55:11 -0500, Rainer Schuetze <[email protected]> wrote:



On 31.01.2013 21:48, Steven Schveighoffer wrote:
On Thu, 31 Jan 2013 15:32:23 -0500, Rainer Schuetze <[email protected]>
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.

You wouldn't be able to do that. The reference count needs to be tail-immutable, meaning the reference count is not immutable, but the data it protects is.

Anticipating your answer, no, there isn't a good way to do this at the moment, it's something D definitely lacks.




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

Right, but where do you get the original pointer from? Wouldn't that reference have incremented the count?

-Steve

Reply via email to