[dmd-concurrency] shutting down
Andrei Alexandrescu
andrei at erdani.com
Thu Jan 21 12:14:45 PST 2010
Sean Kelly wrote:
> Seriously? Is this in server or client apps? In server apps an
> orderly shutdown is generally crucial, since data often has to be
> serialized, logging must be done, etc.
Client. I agree that server shutdown is a very different ballgame, for
example Apache's restart sequence is very elaborate.
>> I only know what I've developed in my projects, and the most
>> effective and robust shutdown I found involved properly cleaning up
>> sockets. Having a shutdown time of a few seconds is not terrible,
>> and allows proper cleanup. I've never had any problems in my
>> applications of having blocking sockets hold up shutdown, because I
>> always write my threads to periodically check for shutdown.
>
> Servers and services generally work this way. In fact there often
> isn't a main() function at all, since it's common to build them as
> shared libraries (Windows NT Services, for example). An event is
> triggered when a shutdown is desired, and the signal must propagate
> through the system. In this case it might be possible to designate
> some main execution loop as the main thread, but I'm frankly
> skeptical that a clean shutdown could be handled magically via the
> method previously described.
I agree we shouldn't put too much faith in the default shutdown method.
I just want to choose a default that's reasonable.
Let me make a counterexample: pthreads have terrible defaults, i.e. it
tears down everything without warning when main() exits. Virtually every
example in Butenhof's book must use calls that override that crappy
default. Look e.g. here:
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);
Not to mention that that Einstein calls exit(0) as the last line in main().
So I'd be happy to define a reasonable default that does allow simple,
meaningful applications to be written, without disallowing people who
write servers from doing what they want to do.
>>> I think it's tenuous to conceive an application that doesn't
>>> terminate when the user wants to terminate it. Even MS Money,
>>> which synchronizes online when you shut down (a weird design if
>>> you ask me) has a "Exit now" button that does what needs to be
>>> done.
>
> Why? Even electronics these days generally don't have an on/off
> switch that's hardwired to the power supply. Instead they send a
> signal that's received by the system and processed in an orderly
> manner. I still have to unplug my laptop and pull the battery to
> shut it down on occasion when it gets wedged in some particularly
> interesting manner.
I agree about the orderly part but let's not get to the point we can't
close an app come hell or high water.
Andrei
More information about the dmd-concurrency
mailing list