Make sure device monitor thread can be cancelled without pthread_cancel
diff --git a/common/thread.c b/common/thread.c
index da7eb21..eb535ab 100644
--- a/common/thread.c
+++ b/common/thread.c
@@ -77,12 +77,12 @@
 int thread_cancel(THREAD_T thread)
 {
 #ifdef WIN32
-	return 0;
+	return -1;
 #else
 #ifdef HAVE_PTHREAD_CANCEL
 	return pthread_cancel(thread);
 #else
-	return 0;
+	return -1;
 #endif
 #endif
 }
diff --git a/src/libusbmuxd.c b/src/libusbmuxd.c
index d971f14..40eefea 100644
--- a/src/libusbmuxd.c
+++ b/src/libusbmuxd.c
@@ -117,6 +117,7 @@
 static struct collection devices;
 static THREAD_T devmon = THREAD_T_NULL;
 static int listenfd = -1;
+static int running = 0;
 static int cancelling = 0;
 
 static volatile int use_tag = 0;
@@ -858,7 +859,6 @@
 
 #ifdef HAVE_INOTIFY
 static int use_inotify = 1;
-
 static int usbmuxd_listen_inotify()
 {
 	int inot_fd;
@@ -1057,7 +1057,7 @@
  */
 static void *device_monitor(void *data)
 {
-	int running = 1;
+	running = 1;
 	collection_init(&devices);
 	cancelling = 0;
 
@@ -1173,7 +1173,9 @@
 		cancelling = 1;
 		socket_shutdown(listenfd, SHUT_RDWR);
 		if (thread_alive(devmon)) {
-			thread_cancel(devmon);
+			if (thread_cancel(devmon) < 0) {
+				running = 0;
+			}
 			res = thread_join(devmon);
 			thread_free(devmon);
 			devmon = THREAD_T_NULL;