From 18347d8325cadd11ce732c7cdcb9b55c727e4938 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 12 Feb 2017 20:11:23 +0100 Subject: [PATCH] Make ImageInput shutdown immediate, instead of waiting for the sleep to time out. --- image_input.cpp | 17 ++++++++++++----- image_input.h | 6 +++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/image_input.cpp b/image_input.cpp index a224af9..5c9d796 100644 --- a/image_input.cpp +++ b/image_input.cpp @@ -246,7 +246,10 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri timespec last_modified = first_modified; struct stat buf; for ( ;; ) { - sleep(1); + { + unique_lock lock(threads_should_quit_mu); + threads_should_quit_modified.wait_for(lock, chrono::seconds(1), []() { return threads_should_quit; }); + } if (threads_should_quit) { return; @@ -275,9 +278,11 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri void ImageInput::shutdown_updaters() { - // TODO: Kick these out of the sleep before one second? - threads_should_quit = true; - + { + unique_lock lock(threads_should_quit_mu); + threads_should_quit = true; + threads_should_quit_modified.notify_all(); + } for (auto &it : update_threads) { it.second.join(); } @@ -286,4 +291,6 @@ void ImageInput::shutdown_updaters() mutex ImageInput::all_images_lock; map> ImageInput::all_images; map ImageInput::update_threads; -volatile bool ImageInput::threads_should_quit = false; +mutex ImageInput::threads_should_quit_mu; +bool ImageInput::threads_should_quit = false; +condition_variable ImageInput::threads_should_quit_modified; diff --git a/image_input.h b/image_input.h index df25a9f..02be497 100644 --- a/image_input.h +++ b/image_input.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -39,7 +40,10 @@ private: static std::mutex all_images_lock; static std::map> all_images; static std::map update_threads; - static volatile bool threads_should_quit; + + static std::mutex threads_should_quit_mu; + static bool threads_should_quit; // Under threads_should_quit_mu. + static std::condition_variable threads_should_quit_modified; // Signals when threads_should_quit is set. }; #endif // !defined(_IMAGE_INPUT_H) -- 2.39.2