scope(exit) and Ctrl-C

Wanderer d-wanderer at hitbts.com
Sat Dec 2 04:28:57 UTC 2017


On Saturday, 2 December 2017 at 03:52:19 UTC, codephantom wrote:
> On Saturday, 2 December 2017 at 00:41:19 UTC, Wanderer wrote:
>> Is there any method to cleanup on Ctrl-C?
>
> // ----------------------------------
> import std.stdio;
> import core.thread;
>
> extern(C) void signal(int sig, void function(int));
> extern(C) void exit(int exit_val);
>
> extern(C) void handle(int sig)
> {
>     writeln("Control-C was pressed..aborting 
> program....goodbye...");
>     // do more stuff?
>     exit(-1);
> }
>
> void main()
> {
>     enum SIGINT = 2;
>     signal(SIGINT,&handle);
>
>     scope (exit){ writeln("Cleanup"); }
>
>     writeln("Waiting...");
>     Thread.sleep(10.seconds);
>     writeln("Done waiting...");
> }
> // ---------------------------------

Thanks! This works. But it seems a little bit suspicions that D's 
type for handler function has `nothrow` `@nogc` and `@system`. I 
wonder why is that?


More information about the Digitalmars-d-learn mailing list