www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Any application shutdown hooks?

reply Matthew Ong <ongbp yahoo.com> writes:
Hi,

If I am writing a database library such as DSL, Data Service Layer, 
there is a need for me to do a good clean up for the connections/results 
sets/transactions that this library is currently handling.

How ever, if another library/package somewhere else calls for a


java.lang.Runtime#public void addShutdownHook(Thread hook)


How do code or what library to use?

can someone please provide a simple working D example for try out.

Of cause, in Even in Java there is event if some cleaning lady were to 
accidentally unplug the main socket of your server. :)


-- 
Matthew Ong
email: ongbp yahoo.com
May 25 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

-Steve
May 25 2011
parent reply Matthew Ong <ongbp yahoo.com> writes:
On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:
 http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

 -Steve

Static Destructor maybe executed when the class is being unloaded GC? Because this is a class level. It is more like the application level exit and such. That is not really the same as: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29 This is triggered when: # The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or # The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown. -- Matthew Ong email: ongbp yahoo.com
May 25 2011
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/25/2011 08:35 AM, Matthew Ong wrote:
 On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:
 http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

 -Steve

Static Destructor maybe executed when the class is being unloaded GC? Because this is a class level. It is more like the application level exit and such.

There is a problem at Digital Mars's site. Steve must have meant the module static destructor. Search for "Static Construction and Destruction" on the module page: http://www.digitalmars.com/d/2.0/module.html (Do not click StaticDestructor at the top of the page. That is linked to the class page.) Ali
May 25 2011
prev sibling next sibling parent Matthew Ong <ongbp yahoo.com> writes:
On 5/25/2011 11:52 PM, Steven Schveighoffer wrote:
 If you want a static dtor that runs on every thread shutdown, use a
 normal static dtor. If you want one that runs on the entire application
 shutdown, use a shared static dtor.

 -Steve

purpose.
If you want one that runs on the entire application shutdown, use a 
shared static dtor.

placing that at the same module dtor and file as the main(string[] args) using template mixin. That might work. I will test that out. -- Matthew Ong email: ongbp yahoo.com
May 25 2011
prev sibling parent reply Matthew Ong <ongbp yahoo.com> writes:
On 5/26/2011 12:23 AM, Andrej Mitrovic wrote:
 On 5/25/11, Steven Schveighoffer<schveiguy yahoo.com>  wrote:
 If you want a static dtor that runs on every thread shutdown, use a normal
 static dtor.  If you want one that runs on the entire application
 shutdown, use a shared static dtor.

I saw some commits change a static dtor to a shared static dtor and wondered what was that all about..

-- Matthew Ong email: ongbp yahoo.com
May 25 2011
parent Matthew Ong <ongbp yahoo.com> writes:
 On 5/25/11, Steven Schveighoffer<schveiguy yahoo.com> wrote:


 normal
 static dtor. If you want one that runs on the entire application
 shutdown, use a shared static dtor.

I saw some commits change a static dtor to a shared static dtor and wondered what was that all about..


do that exactly. others may be dll and could be unloaded and cause __dtor to be called. Will try such direction. Thanks people. -- Matthew Ong email: ongbp yahoo.com
May 26 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 25 May 2011 11:35:51 -0400, Matthew Ong <ongbp yahoo.com> wrote:

 On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:
 http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

 -Steve

Static Destructor maybe executed when the class is being unloaded GC? Because this is a class level. It is more like the application level exit and such.

This is *exactly* what static destructors do. "A static destructor gets called on program termination" Actually, that description is inaccurate for non-shared static dtors, someone should fix that.
 That is not really the same as:
 http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29

If you want a static dtor that runs on every thread shutdown, use a normal static dtor. If you want one that runs on the entire application shutdown, use a shared static dtor. -Steve
May 25 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 25 May 2011 11:50:16 -0400, Ali Çehreli <acehreli yahoo.com> wrote:

 On 05/25/2011 08:35 AM, Matthew Ong wrote:
 On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:
 http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

 -Steve

Static Destructor maybe executed when the class is being unloaded GC? Because this is a class level. It is more like the application level exit and such.

There is a problem at Digital Mars's site. Steve must have meant the module static destructor. Search for "Static Construction and Destruction" on the module page: http://www.digitalmars.com/d/2.0/module.html (Do not click StaticDestructor at the top of the page. That is linked to the class page.)

Oh, yeah, that's exactly what I did! hm... Need to file a bug. (filed as http://d.puremagic.com/issues/show_bug.cgi?id=6055 ) Note that class static dtors are exactly the same as module static dtors, they just are inside the class namespace. -Steve
May 25 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 25 May 2011 11:52:14 -0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 On Wed, 25 May 2011 11:35:51 -0400, Matthew Ong <ongbp yahoo.com> wrote:

 On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:
 http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

 -Steve

Static Destructor maybe executed when the class is being unloaded GC? Because this is a class level. It is more like the application level exit and such.

This is *exactly* what static destructors do. "A static destructor gets called on program termination" Actually, that description is inaccurate for non-shared static dtors, someone should fix that.
 That is not really the same as:
 http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29

If you want a static dtor that runs on every thread shutdown, use a normal static dtor. If you want one that runs on the entire application shutdown, use a shared static dtor.

I should clarify that when I wrote this, I thought the description was from the module.html file, not the class.html file. Ali pointed out the bug in the web site. So I didn't get why you were talking about classes, sorry :) Class and module static ctor/dtors should be exactly what you are looking for. -Steve
May 25 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 5/25/11, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 If you want a static dtor that runs on every thread shutdown, use a normal
 static dtor.  If you want one that runs on the entire application
 shutdown, use a shared static dtor.

I saw some commits change a static dtor to a shared static dtor and wondered what was that all about..
May 25 2011