DIP60: @nogc attribute

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 19 16:48:29 PDT 2014


Am Wed, 16 Apr 2014 20:32:20 +0000
schrieb "Peter Alexander" <peter.alexander.au at gmail.com>:

> On Wednesday, 16 April 2014 at 20:29:17 UTC, bearophile wrote:
> > Peter Alexander:
> >
> >> (I assume that nothrow isn't meant to be there?)
> >
> > In D nothrow functions can throw errors.
> 
> Of course, ignore me :-)
> 
> 
> >> You could do something like this:
> >>
> >> void foo() @nogc
> >> {
> >>    static err = new Error();
> >>    if (badthing)
> >>    {
> >>        err.setError("badthing happened");
> >>        throw err;
> >>    }
> >> }
> >
> > To be mutable err also needs to be __gshared.
> 
> But then it isn't thread safe. Two threads trying to set and 
> throw the same Error is a recipe for disaster.

Also: As far as I remember from disassembling C++, static
variables in functions are initialized on first access and
guarded by a bool. The first call to foo() would execute
"err = new Error();" in that case. This code should not
compile under @nogc.

-- 
Marco



More information about the Digitalmars-d mailing list