Signals

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Wed May 27 07:07:28 PDT 2015


The way I do it is to set a global variable in the signal 
handler, then have the loop watch for it. So like

__gshared bool terminated = false;
extern(C) void handleSigTerm(int) { terminated = true; }

void main() {
     while(!terminated) {
           if(select() < 0)
              if(errno == EINTR) continue; // let the loop check 
the flag again
           // the rest of the stuff
     }

     // do normal cleanup here, and as the functions return
     // naturally they will do unwinding.
}



Of course, with this strategy, if there's some long operation 
running inside the loop it won't get a chance to check the 
flag.... how you handle that is up to you. Maybe check that flag 
from time to time and throw an exception from work functions if 
it is set.


But there's no convenient way to exit from the handler itself 
while doing unwinding. You can exit, sure (use the C exit 
function), and the OS will clean up a lot of stuff but D 
destructors won't be called that way.


More information about the Digitalmars-d mailing list