comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Should weak counted reference be able to be strengthened?
Date: Fri, 21 Nov 2014 14:16:01 +0100
Date: 2014-11-21T14:16:01+01:00	[thread overview]
Message-ID: <7d9r3whe43z8$.1f2l3n5cz0zh0$.dlg@40tude.net> (raw)
In-Reply-To: slrnm6u770.nrc.lithiumcat@nat.rebma.instinctive.eu

On Fri, 21 Nov 2014 11:07:07 +0000 (UTC), Natasha Kerensikova wrote:

> So my question is, now that I am considering extending my
> reference-counting system to include weak reference, do I really needed
> a public interface to build a strong reference from a weak one?

Yes.

> Or is it enough to have weak reference primitives that return Accessor
> and Mutator objects, thereby allowing safe access without allowing a
> weak reference to be promoted into a strong one?

How this is supposed to work? The only safe referential object is a strong
reference.

> In all the use cases I can imagine, the weak reference is only used to
> access the referred object. I can't make up an example where a strong
> reference would be needed from only a weak one. Am I missing something?

Yes. There are many cases when weak references are components of containers
or when weak references are used in composite objects which change their
content upon disappearance of the referenced object.

> The interesting point I see in forbidding such a "reference
> strengthening" is to prevent some cases circular strong references,
> which is the problem weak references are supposed to solve.

When a weak reference is promoted to a strong one that occurs on a nested
context. Such strong references are allocated on the stack. Therefore there
is no any danger of getting circular dependencies unless the structure of
references in objects is already circular. In short, there is nothing to
worry about.

> As a side note, I'm a bit surprised by how easy weak/strong reference
> seem to be: I think I would only need to store two counters instead of
> one, one for the strong references and one for all references,
> deallocating the object when the strong reference counter reaches
> zero, and freeing the counters when the total counter reaches zero -- as
> if each weak reference was a strong reference to the counters. Then it's
> only a matter of adding an atomic increase-if-not-zero to the strong
> reference counter to build accessors from weak references.
> Is there a subtle trap I'm missing?

Weak references usually require a notification mechanism when the object
disappears. Thus you need a count and the head of the doubly-linked list of
the weak references pointing to the object.

In some complicated design you may need an identification of the holder of
a read-write strong reference. That is when referenced objects are mutated
while accessed in an immutable way in parallel. This is usually the case
when manipulating structures like graphs and trees. A read-write reference
clones the object if this is the first reference and works with already
existing copy if this is the second reference from the same context.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  reply	other threads:[~2014-11-21 13:16 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-21 11:07 Should weak counted reference be able to be strengthened? Natasha Kerensikova
2014-11-21 13:16 ` Dmitry A. Kazakov [this message]
2014-11-21 15:00   ` Natasha Kerensikova
2014-11-21 16:50     ` Dmitry A. Kazakov
2014-11-21 18:24       ` Natasha Kerensikova
2014-11-21 19:36         ` Dmitry A. Kazakov
2014-11-21 22:28           ` Randy Brukardt
2014-11-22  9:57           ` Natasha Kerensikova
2014-11-22 10:19             ` Dmitry A. Kazakov
2014-11-22 10:36               ` Natasha Kerensikova
2014-11-22 11:15                 ` Dmitry A. Kazakov
2014-11-21 22:39       ` Randy Brukardt
2014-11-21 22:49         ` Shark8
2014-11-22  8:42           ` Dmitry A. Kazakov
2014-11-22  8:42         ` Dmitry A. Kazakov
2014-11-24 22:15           ` Randy Brukardt
2014-11-25  8:36             ` Dmitry A. Kazakov
2014-11-25 21:49               ` Randy Brukardt
2014-11-26  9:28                 ` Dmitry A. Kazakov
2014-11-26 17:51                   ` brbarkstrom
2014-11-26 21:09                   ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox