浏览代码

Properly quit server thread on app exit.

Sascha Zelzer 14 年之前
父节点
当前提交
ab8f6f6939

+ 12 - 4
Plugins/org.commontk.dah.core/ctkSoapConnectionRunnable.cpp

@@ -28,8 +28,9 @@
 
 //----------------------------------------------------------------------------
 ctkSoapConnectionRunnable::ctkSoapConnectionRunnable(int socketDescriptor)
-  : socketDescriptor(socketDescriptor)
+  : socketDescriptor(socketDescriptor), isAboutToQuit(0)
 {
+  connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()));
 }
 
 //----------------------------------------------------------------------------
@@ -48,14 +49,16 @@ void ctkSoapConnectionRunnable::run()
     return;
     }
 
-  while (tcpSocket.state() == QTcpSocket::ConnectedState)
+  const int timeout = 1 * 1000;
+  while (tcpSocket.state() == QTcpSocket::ConnectedState &&
+         isAboutToQuit.fetchAndAddOrdered(0) == 0)
     {
-    //const int timeout = 5 * 1000;
 
-    tcpSocket.waitForReadyRead(-1);
+    tcpSocket.waitForReadyRead(timeout);
 
     readClient(tcpSocket);
     }
+
 }
 
 //----------------------------------------------------------------------------
@@ -151,3 +154,8 @@ void ctkSoapConnectionRunnable::readClient(QTcpSocket& socket)
       }
     }
 }
+
+void ctkSoapConnectionRunnable::aboutToQuit()
+{
+  isAboutToQuit.testAndSetOrdered(0, 1);
+}

+ 6 - 0
Plugins/org.commontk.dah.core/ctkSoapConnectionRunnable_p.h

@@ -45,12 +45,18 @@ signals:
   void incomingSoapMessage(const QtSoapMessage& message, QtSoapMessage* reply);
   void incomingWSDLMessage(const QString& message, QString* reply);
 
+protected slots:
+
+  void aboutToQuit();
+
 private:
 
   void readClient(QTcpSocket& socket);
 
   int socketDescriptor;
 
+  QAtomicInt isAboutToQuit;
+
 };
 
 #endif // CTKSOAPCONNECTIONRUNNABLE_P_H