--- a/libupnpp/device.cxx
+++ b/libupnpp/device.cxx
@@ -57,7 +57,7 @@
 
 UpnpDevice::UpnpDevice(const string& deviceId, 
                        const unordered_map<string, string>& xmlfiles)
-    : m_deviceId(deviceId)
+    : m_deviceId(deviceId), m_needExit(false)
 {
     //LOGDEB("UpnpDevice::UpnpDevice(" << m_deviceId << ")" << endl);
 
@@ -352,7 +352,9 @@
         PTMutexLocker lock(cblock);
         int err = pthread_cond_timedwait(&evloopcond, lock.getMutex(), 
                                          &wkuptime);
-        if (err && err != ETIMEDOUT) {
+        if (m_needExit) {
+            break;
+        } else if (err && err != ETIMEDOUT) {
             LOGINF("UpnpDevice:eventloop: wait errno " << errno << endl);
             break;
         } else if (err == 0) {
@@ -403,3 +405,9 @@
     pthread_cond_broadcast(&evloopcond);
 }
 
+void UpnpDevice::shouldExit()
+{
+    m_needExit = true;
+    pthread_cond_broadcast(&evloopcond);
+}
+