From 9dad00ec2c040e19597db6bc64d2986753f32797 Mon Sep 17 00:00:00 2001 From: sigbjorn Date: Tue, 4 Apr 2017 20:28:19 +0200 Subject: [PATCH] Provide cleanup for timer_global_clock singleton, issue #505 (#512) * temporary workaround for python hanging on dlib::shared_ptr_thread_safe when extension-module using dlib is unloaded on windows platform * added some more doc and tests for the new delete_global_clock function. Minor adjustment to singleton pattern to allow easy access to singleton shared ptr * alternate _WIN32 specific solution, with no extra functions nor user-actions needed * removed floating/obsolete delete_global_clock, tab to space * leaving linux code unchanged in static desctructor * cleanup not modified files * align formatting --- dlib/timer/timer.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dlib/timer/timer.cpp b/dlib/timer/timer.cpp index f5d4a544b..5b5882ae9 100644 --- a/dlib/timer/timer.cpp +++ b/dlib/timer/timer.cpp @@ -24,11 +24,38 @@ namespace dlib timer_global_clock:: ~timer_global_clock() { + // The only time this destructor is called is when + // + // a) the process terminates + // b) the dynamic library(.so/.dll) is unloaded (could be a part of a)) + // + // in case of a) + // windows: the process termination is especially painful, since threads are killed + // before destructors of the process image .dll's are called. + // Thus, for the windows platform, there is no threads running, so the only thing + // to do here is just let the standard memberwise destructors run + // linux: it's ok to just signal shutdown and wait for the running thread, to exit + // + // in case of b) + // windows: + // if it's part of the termination process, a) applies + // if its part of user doing manual load_library/unload_library + // there is no (safe/robust)solution, but best practices are described here + // https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971.aspx + // to support such a clean shutdown, you are required to make a call prior to + // unload dll, that shutdown all the threads in the contained dll. + // This could be done in this module by providing a global_delete_clock() + // + // linux: the destructor for linux will do it's usual job regardless. + // + + #ifndef _WIN32 m.lock(); shutdown = true; s.signal(); m.unlock(); wait(); + #endif } // ----------------------------------------------------------------------------------------