Thread overview
Re: GC conservatism -- again
Dec 30, 2010
Sean Kelly
Dec 30, 2010
Sean Kelly
Dec 30, 2010
Sean Kelly
Dec 30, 2010
Simen kjaeraas
December 30, 2010
Adam Ruppe Wrote:

> On 12/27/10, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> > What about tools to make deallocation easier?  For example, we have scope(exit) that you could potentially use to ensure a memory block is deallocated on exit from a scope, what about a thread exit?
> 
> It seems to me that the simplest thing might simply be a list of delegates stored with the thread:
> 
> thread.onexit ~= { free(whatever); };

Already possible via static dtors.

December 30, 2010
On 12/30/10 11:19 AM, Sean Kelly wrote:
> Adam Ruppe Wrote:
>
>> On 12/27/10, Steven Schveighoffer<schveiguy@yahoo.com>  wrote:
>>> What about tools to make deallocation easier?  For example, we have
>>> scope(exit) that you could potentially use to ensure a memory block is
>>> deallocated on exit from a scope, what about a thread exit?
>>
>> It seems to me that the simplest thing might simply be a list of delegates stored
>> with the thread:
>>
>> thread.onexit ~= { free(whatever); };
>
> Already possible via static dtors.

Not dynamically...

Andrei
December 30, 2010
Andrei Alexandrescu Wrote:

> On 12/30/10 11:19 AM, Sean Kelly wrote:
> > Adam Ruppe Wrote:
> >
> >> On 12/27/10, Steven Schveighoffer<schveiguy@yahoo.com>  wrote:
> >>> What about tools to make deallocation easier?  For example, we have scope(exit) that you could potentially use to ensure a memory block is deallocated on exit from a scope, what about a thread exit?
> >>
> >> It seems to me that the simplest thing might simply be a list of delegates stored with the thread:
> >>
> >> thread.onexit ~= { free(whatever); };
> >
> > Already possible via static dtors.
> 
> Not dynamically...

void*[] toFree;

static ~this() {
    foreach(e; toFree)
        free(e);
}

What am I missing?
December 30, 2010
On 12/30/10 12:38 PM, Sean Kelly wrote:
> Andrei Alexandrescu Wrote:
>
>> On 12/30/10 11:19 AM, Sean Kelly wrote:
>>> Adam Ruppe Wrote:
>>>
>>>> On 12/27/10, Steven Schveighoffer<schveiguy@yahoo.com>   wrote:
>>>>> What about tools to make deallocation easier?  For example, we have
>>>>> scope(exit) that you could potentially use to ensure a memory block is
>>>>> deallocated on exit from a scope, what about a thread exit?
>>>>
>>>> It seems to me that the simplest thing might simply be a list of delegates stored
>>>> with the thread:
>>>>
>>>> thread.onexit ~= { free(whatever); };
>>>
>>> Already possible via static dtors.
>>
>> Not dynamically...
>
> void*[] toFree;
>
> static ~this() {
>      foreach(e; toFree)
>          free(e);
> }
>
> What am I missing?

I'm thinking of an API that allows people to dynamically add "to do" stuff, a la C's atexit(). Yours above is dynamic (because client code can append to toFree) but is hand-written by the client.

Andrei
December 30, 2010
Andrei Alexandrescu Wrote:

> On 12/30/10 12:38 PM, Sean Kelly wrote:
> > Andrei Alexandrescu Wrote:
> >
> >> On 12/30/10 11:19 AM, Sean Kelly wrote:
> >>> Adam Ruppe Wrote:
> >>>
> >>>> On 12/27/10, Steven Schveighoffer<schveiguy@yahoo.com>   wrote:
> >>>>> What about tools to make deallocation easier?  For example, we have scope(exit) that you could potentially use to ensure a memory block is deallocated on exit from a scope, what about a thread exit?
> >>>>
> >>>> It seems to me that the simplest thing might simply be a list of delegates stored with the thread:
> >>>>
> >>>> thread.onexit ~= { free(whatever); };
> >>>
> >>> Already possible via static dtors.
> >>
> >> Not dynamically...
> >
> > void*[] toFree;
> >
> > static ~this() {
> >      foreach(e; toFree)
> >          free(e);
> > }
> >
> > What am I missing?
> 
> I'm thinking of an API that allows people to dynamically add "to do" stuff, a la C's atexit(). Yours above is dynamic (because client code can append to toFree) but is hand-written by the client.

Hand written but trivial.  It would be easy enough to add the feature to core.runtime or core.thread though.
December 30, 2010
Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:

> I'm thinking of an API that allows people to dynamically add "to do" stuff, a la C's atexit(). Yours above is dynamic (because client code can append to toFree) but is hand-written by the client.

So like this:


import std.stdio;

void delegate()[] terminators;

void atExit( void delegate() dg ) {
    terminators ~= dg;
}

static ~this( ) {
    foreach ( t; terminators ) {
        t( );
    }
}

void main( ) {
    atExit( {writeln("OH HAI! I BROKE UR THREAD");} );
}


-- 
Simen