Proper way to exit with specific exit code?
Ruby The Roobster
rubytheroobster at yandex.com
Sun Nov 13 23:24:34 UTC 2022
On Sunday, 13 November 2022 at 21:16:32 UTC, mw wrote:
> On Saturday, 19 September 2020 at 06:11:15 UTC, Jacob Carlborg
> wrote:
>> On 2020-09-17 16:58, 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 proper way is:
>>
>> int main()
>> {
>> return 42;
>> }
>>
>> I highly recommend against trying to terminate the application
>> using `exit` or any other way. Just let the control flow
>> return back to the `main` function.
>
> I'm facing this problem to exit early from a multi threaded
> program for mem profiling purpose:
>
> https://forum.dlang.org/thread/zbdevevgghtdgfryuymh@forum.dlang.org
>
>
> So what the simplest and reliable way to terminate all threads
> and exit to os?
>
> I even tried core.stdc.stdlib.exit(-1), it does not work.
Just give the threads a killswitch, pass the killswitch method to
all of the threads, wait for all the threads to exit, and then
return. If this doesn't work, than I don't know what will.
More information about the Digitalmars-d-learn
mailing list