Proper way to exit with specific exit code?

IGotD- nise at
Fri Sep 18 08:20:59 UTC 2020

On Friday, 18 September 2020 at 05:02:21 UTC, H. S. Teoh wrote:
> That's the obvious solution, except that actually implementing 
> it is not so simple.  When you have multiple threads listening 
> for each other and/or doing work, there is no 100% guaranteed 
> way of cleanly shutting all of them down at the same time.  You 
> can't just clean up the calling thread and leave the others 
> running, because the other threads might hold references to 
> your data, etc..  But there's no universal protocol for 
> shutting down the other threads too -- they could be in a busy 
> loop with some long-running computation, or they may not be 
> checking for thread messages, or they could be in a server loop 
> that is designed to keep running, etc..  It's one of those 
> annoying things that reduce to the halting problem in the 
> general case.
> Unless we adopt some kind of exit protocol that will apply to 
> *all* threads in *all* D programs, I don't see any way to 
> implement something that will work in the general case.
> T

I think a pragmatic solution is just to mutex protect the D exit 
function in case several threads tries to use simultaneously. 
Then if more threads call exit, it will do nothing as the first 
one that called exit actually do the tear down.

Also, it should be responsibility of the program to ensure that 
its tear down code runs before calling the D exit function. 
That's the only way I can think of because waiting for all other 
threads to release their resources and exit isn't really 
realistic either as that might do that the program exit never 
happens. Whatever you do you, you have to resort to some "manual" 

I suggest keeping it simple and stupid.

More information about the Digitalmars-d-learn mailing list