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