An Issue I Wish To Raise Awareness On

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 20 14:20:46 PDT 2017


On Thursday, July 20, 2017 07:40:35 Dominikus Dittes Scherkl via 
Digitalmars-d wrote:
> On Wednesday, 19 July 2017 at 22:35:43 UTC, Jonathan M Davis
>
> wrote:
> > The issue isn't the object being destroyed. It's what it refers
> > to via its member variables. For instance, what if an object
> > were to remove itself from a shared list when it's destroyed
> > (e.g. because it's an observer in the observer pattern). The
> > object has a reference to the list, but it doesn't own it.
>
> So, even a thread-local object that has references to a shared
> list
> has to handle those as shared, even in its non-shared destructor.
> I can't follow your argument.

You can't just strip off shared. To do so defeats the purpose of shared. If
you have something like

struct S
{
     shared List<Foo> _list;

     ~this()
     {
        ...
     }
}

then inside of the destructor, _list is not treated as shared, meaning that
none of the compiler protections for shared are in place, no locking has
occurred, and the compiler is free to make optimizations based on the wrong
assumption that all of the member variables are thread-local. If nothing
else has access to that list, then it'll work, but if anything else does -
and if it's a reference type, that's perfectly possible - then you have a
threading problem, because shared has been violated.

Except in cases where the member variables are all value types and thus no
other references to them should exist when the destructor is called,
stripping away shared from them means that the compiler can no longer
properly enforce shared, and it's going to make the wrong assumptions about
whether the data can be treated as thread-local or not.

If we go with the assumption that nothing has pointers to the member
variables (since doing so would be @system, and they're only valid so long
as the struct isn't moved anyway), you can probably strip off the outer
layer of shared safely in the destructor, but if you're dealing with a
reference type, anything it points to needs to still be treated as shared.

- Jonathan M Davis



More information about the Digitalmars-d mailing list