From ab9dcfbd356bfa2e2054309ea0fdab31f9180497 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 1 Nov 2015 12:36:23 +0100 Subject: [PATCH] Clean learner-thread termination on program exit --- gui/NeuroUI/netlearner.cpp | 9 ++++++++- gui/NeuroUI/netlearner.h | 6 ++++++ gui/NeuroUI/neuroui.cpp | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/gui/NeuroUI/netlearner.cpp b/gui/NeuroUI/netlearner.cpp index b0956e1..3e7d5f9 100644 --- a/gui/NeuroUI/netlearner.cpp +++ b/gui/NeuroUI/netlearner.cpp @@ -24,7 +24,7 @@ void NetLearner::run() timer.start(); size_t numIterations = 100000; - for (size_t iteration = 0; iteration < numIterations; ++iteration) + for (size_t iteration = 0; iteration < numIterations && cancel == false; ++iteration) { auto trainingSample = mnistLoader.getRandomSample(); @@ -76,4 +76,11 @@ void NetLearner::run() logString.append(ex.what()); emit logMessage(logString); } + + cancel = false; +} + +void NetLearner::cancelLearning() +{ + cancel = true; } diff --git a/gui/NeuroUI/netlearner.h b/gui/NeuroUI/netlearner.h index 70378a4..018c3bf 100644 --- a/gui/NeuroUI/netlearner.h +++ b/gui/NeuroUI/netlearner.h @@ -7,6 +7,9 @@ class NetLearner : public QThread { Q_OBJECT +private: + bool cancel = false; + private: void run() Q_DECL_OVERRIDE; @@ -15,6 +18,9 @@ signals: void progress(double progress); void currentNetError(double error); void sampleImageLoaded(const QImage &image); + +public slots: + void cancelLearning(); }; #endif // NETLEARNER_H diff --git a/gui/NeuroUI/neuroui.cpp b/gui/NeuroUI/neuroui.cpp index c057984..b366c65 100644 --- a/gui/NeuroUI/neuroui.cpp +++ b/gui/NeuroUI/neuroui.cpp @@ -12,6 +12,12 @@ NeuroUI::NeuroUI(QWidget *parent) : NeuroUI::~NeuroUI() { + if (m_netLearner != nullptr) + { + m_netLearner->cancelLearning(); + m_netLearner->wait(); + } + delete ui; }