Daemon Threads

Sean Kelly sean at invisibleduck.org
Tue Aug 7 22:11:42 PDT 2012


On Aug 7, 2012, at 4:01 PM, David <d at dav1d.de> wrote:

>> Daemon threads will keep running until the process terminates.Forcibly halting them any earlier risks leaving mutexes they hold in a 
> locked state, etc, which could break shutdown and hang the app. Typically, you'll want to have a shared module dtor communicate to any daemon threads that it's time for them to halt, and block until a response is received if you want to ensure a clean shutdown.
> 
> Ok, so it's not like e.g. in Python, if the main thread terminates, all daemons die and the process exits?
> 
> Let's say I have a static shared dtor, which communicates to all daemons, hey you have to shutdown, now! But what happens if an exception is thrown, will the dtor still be called? And how would you communicate with the different threads?

The basic shutdown process is:

1. Wait for all non-daemon threads to complete.
2. Run static dtors.
3. Terminate the GC.
4. Exit.

We let the OS kill any daemon threads when the process exits.  We could forcibly terminate them after step 3, but it amounts to the same thing.


More information about the Digitalmars-d-learn mailing list