diff --git a/core/src/config.cpp b/core/src/config.cpp index 6a5fc314..cb57cf56 100644 --- a/core/src/config.cpp +++ b/core/src/config.cpp @@ -48,13 +48,17 @@ void ConfigManager::save(bool lock) { void ConfigManager::enableAutoSave() { if (!autoSaveEnabled) { autoSaveEnabled = true; + termFlag = false; autoSaveThread = std::thread(autoSaveWorker, this); } } void ConfigManager::disableAutoSave() { if (autoSaveEnabled) { + std::unique_lock lock(termMtx); autoSaveEnabled = false; + termFlag = true; + termCond.notify_one(); autoSaveThread.join(); } } @@ -80,6 +84,11 @@ void ConfigManager::autoSaveWorker(ConfigManager* _this) { _this->save(false); } _this->mtx.unlock(); - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + // Sleep but listen for wakeup call + { + std::unique_lock lock(_this->termMtx); + _this->termCond.wait_for(lock, std::chrono::milliseconds(1000), [_this]() { return _this->termFlag; } ); + } } } \ No newline at end of file diff --git a/core/src/config.h b/core/src/config.h index 277d7a44..697e4602 100644 --- a/core/src/config.h +++ b/core/src/config.h @@ -29,4 +29,8 @@ private: std::thread autoSaveThread; std::mutex mtx; + std::mutex termMtx; + std::condition_variable termCond; + bool termFlag = false; + }; \ No newline at end of file