Proper way to exit with specific exit code?

IGotD- nise at nise.com
Thu Sep 17 16:46:05 UTC 2020


On Thursday, 17 September 2020 at 14:58:48 UTC, drathier wrote:
> What's the proper way to exit with a specific exit code?
>
> I found a bunch of old threads discussing this, making sure 
> destructors run and the runtime terminates properly, all of 
> which seemingly concluding that it's sad that there isn't a way 
> to do this easily, but hopefully things have changed in the 
> last 5-10 years and I'm just missing the obvious solution.

The only way is to return from main. The thing is that druntime 
runs initialization before main and then returning from main it 
runs all the tear down code including cleaning up the GC. This 
means there is no equivalent of the exit function in the C 
library. Calling exit from D means that there will be no cleanup 
in D environment.

This is a bit limiting for my needs for example. I would like 
that exiting from main will not tear down the D runtime because 
my system is a message driven system and main just sets up the 
program and then returns but the programs continues to react on 
messages. Many libraries like Qt circumvents this just by parking 
the main thread as a event handler but this doesn't fit my system 
and will waste one thread resource. Finally to exit the program I 
have equivalent to the C library exit function. Creating a 
similar exit function in D would be trivial really.


More information about the Digitalmars-d-learn mailing list