Clean learner-thread termination on program exit

This commit is contained in:
Michael Mandl 2015-11-01 12:36:23 +01:00
parent 1a0d2b9ea7
commit ab9dcfbd35
3 changed files with 20 additions and 1 deletions

View file

@ -24,7 +24,7 @@ void NetLearner::run()
timer.start(); timer.start();
size_t numIterations = 100000; 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(); auto trainingSample = mnistLoader.getRandomSample();
@ -76,4 +76,11 @@ void NetLearner::run()
logString.append(ex.what()); logString.append(ex.what());
emit logMessage(logString); emit logMessage(logString);
} }
cancel = false;
}
void NetLearner::cancelLearning()
{
cancel = true;
} }

View file

@ -7,6 +7,9 @@ class NetLearner : public QThread
{ {
Q_OBJECT Q_OBJECT
private:
bool cancel = false;
private: private:
void run() Q_DECL_OVERRIDE; void run() Q_DECL_OVERRIDE;
@ -15,6 +18,9 @@ signals:
void progress(double progress); void progress(double progress);
void currentNetError(double error); void currentNetError(double error);
void sampleImageLoaded(const QImage &image); void sampleImageLoaded(const QImage &image);
public slots:
void cancelLearning();
}; };
#endif // NETLEARNER_H #endif // NETLEARNER_H

View file

@ -12,6 +12,12 @@ NeuroUI::NeuroUI(QWidget *parent) :
NeuroUI::~NeuroUI() NeuroUI::~NeuroUI()
{ {
if (m_netLearner != nullptr)
{
m_netLearner->cancelLearning();
m_netLearner->wait();
}
delete ui; delete ui;
} }