Clean process exit

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue May 12 06:21:53 PDT 2015


On 5/7/15 12:15 PM, Dmitri wrote:
> I'm a D noob, so my question is perhaps naive. At work, we develop
> servers that are 24/7 and as such they don't have a clean exit path.
> Sometimes, however, I'd like to troubleshoot a process and use valgrind,
> in which case I have to come with a way of forcing a program's exit.
>
> I know I can control (some) valgrind tools from outside by forcing a
> log, but it is not always the case. So more often than not, I end up
> adding some sort of debugging code to be able to force an exit from
> outside - in which case it is as simple as calling stdlib's exit. This
> poses a problem when the exit is called from a thread other than the
> main one, since it did not have a _d_dos_registry on the way "in", but
> it gets called on the way "out" and crashes when the _tlsRanges is
> modified.
>
> My questions are,
>    * is it legit to want to terminate not from the main thread?
>    * if not, what is the recommended simpler way of doing it?

In all my server type multi-threaded apps, I do something like this:

while(true) => while(!exiting) // exiting is some global bool

waitForInput(forever) => waitForInput(1000ms)

Then in the signal handler, I set exiting = true, nothing else.

All the threads exit gracefully, and the main thread joins all the 
others. Max time to shut down is 1 second.

Note that one should always handle shutdown when asked by the OS (either 
through signals or whatever mechanism the OS uses) so the OS can 
gracefully shut down.

-Steve


More information about the Digitalmars-d mailing list