From 45116d88570f7ad19eed03b43596d328f49a8fa9 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 17 Jul 2020 18:35:38 +0200 Subject: [PATCH] Add mutex to protect update thread --- AdaptiveBrightness/main_window.cpp | 8 ++++++++ AdaptiveBrightness/main_window.hpp | 2 ++ 2 files changed, 10 insertions(+) diff --git a/AdaptiveBrightness/main_window.cpp b/AdaptiveBrightness/main_window.cpp index fe31789..1c773d1 100644 --- a/AdaptiveBrightness/main_window.cpp +++ b/AdaptiveBrightness/main_window.cpp @@ -61,6 +61,8 @@ void MainWindow::enumSensors() void MainWindow::enumMonitors() { + m_mtx.lock(); + m_ui.monitorDropdown->clear(); auto monitors = enumerateMonitors(); @@ -70,6 +72,8 @@ void MainWindow::enumMonitors() m_ui.monitorDropdown->addItem(monitor.getName()); m_monitors.push_back({std::move(monitor), 0.f, 1.f, currentBrightness, false}); } + + m_mtx.unlock(); } void MainWindow::loadIcons() @@ -269,6 +273,8 @@ void MainWindow::updateCurrentMonitorGUI(int index) void MainWindow::updateState() { + m_mtx.lock(); + qDebug(ltr("Updating state")); bool sensorError = false; @@ -322,6 +328,8 @@ void MainWindow::updateState() if(m_trayIcon) { m_trayIcon->setToolTip(tr("AdaptiveBrightness - %1%").arg(brightness * 100, 0, 'f', 0)); } + + m_mtx.unlock(); } bool MainWindow::setMonitorBrightness(float& brightness) diff --git a/AdaptiveBrightness/main_window.hpp b/AdaptiveBrightness/main_window.hpp index dd7608e..7383aed 100644 --- a/AdaptiveBrightness/main_window.hpp +++ b/AdaptiveBrightness/main_window.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +64,7 @@ class MainWindow : public QMainWindow { QThread* m_timerThread = nullptr; QTimer* m_timer = nullptr; + QMutex m_mtx; struct SensorData { Sensor driver;