# Gavin Andresen # 2013-09-04 04:25:14 # https://bitcointalk.org/index.php?topic=149479.msg3077266#msg3077266 I wrote a big comment in init.cpp describing how shutdown works: @p{par} Code: // @p{brk} // Thread management and startup/shutdown: @p{brk} // @p{brk} // The network-processing threads are all part of a thread group @p{brk} // created by AppInit() or the Qt main() function. @p{brk} // @p{brk} // A clean exit happens when StartShutdown() or the SIGTERM @p{brk} // signal handler sets fRequestShutdown, which triggers @p{brk} // the DetectShutdownThread(), which interrupts the main thread group. @p{brk} // DetectShutdownThread() then exits, which causes AppInit() to @p{brk} // continue (it .joins the shutdown thread). @p{brk} // Shutdown() is then @p{brk} // called to clean up database connections, and stop other @p{brk} // threads that should only be stopped after the main network-processing @p{brk} // threads have exited. @p{brk} // @p{brk} // Note that if running -daemon the parent process returns from AppInit2 @p{brk} // before adding any threads to the threadGroup, so .join_all() returns @p{brk} // immediately and the parent exits from main(). @p{brk} // @p{brk} // Shutdown for Qt is very similar, only it uses a QTimer to detect @p{brk} // fRequestShutdown getting set, and then does the normal Qt @p{brk} // shutdown thing. @p{brk} // @p{brk} @p{brk} I don't know nuthin about Windows' SetConsoleCtrlHandler(), but just setting the fRequestShutdown global variable should do the right thing. @p{brk}