From a76a7a6df3b1bdb6dbd6e8c1c970271ae3b4b9b2 Mon Sep 17 00:00:00 2001
From: Mark Watkins <jedimark@users.sourceforge.net>
Date: Thu, 1 Sep 2011 13:43:47 +1000
Subject: [PATCH] Better thread shutdown

---
 Graphs/gGraphView.cpp | 16 +++++++++++-----
 Graphs/gGraphView.h   |  2 ++
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/Graphs/gGraphView.cpp b/Graphs/gGraphView.cpp
index faa3754b..4c3164d4 100644
--- a/Graphs/gGraphView.cpp
+++ b/Graphs/gGraphView.cpp
@@ -296,12 +296,23 @@ gThread::gThread(gGraph *g)
     graph=g;
     mutex.lock();
 }
+gThread::~gThread()
+{
+    if (isRunning()) {
+        m_running=false;
+        mutex.unlock();
+        wait();
+        terminate();
+    }
+}
+
 void gThread::run()
 {
     m_running=true;
     while (m_running) {
         //mutex.lock();
         if (mutex.tryLock(500)) {
+            if (!m_running) break;
             int originX=m_lastbounds.x();
             int originY=m_lastbounds.y();
             int width=m_lastbounds.width();
@@ -346,11 +357,6 @@ gGraph::gGraph(gGraphView *graphview,QString title,int height,short group) :
 }
 gGraph::~gGraph()
 {
-    if (m_thread->isRunning()) {
-        m_thread->die();
-        m_thread->wait();
-        m_thread->exit();
-    }
     delete m_thread;
     delete quad;
 }
diff --git a/Graphs/gGraphView.h b/Graphs/gGraphView.h
index 1539591f..9a7cf48e 100644
--- a/Graphs/gGraphView.h
+++ b/Graphs/gGraphView.h
@@ -161,6 +161,8 @@ class gThread:public QThread
 {
 public:
     gThread(gGraph *g);
+    ~gThread();
+
     void run();
     void paint(int originX, int originY, int width, int height);
     void die() { m_running=false; }