Index: motion.c
===================================================================
--- motion.c	(revisión: 230)
+++ motion.c	(copia de trabajo)
@@ -505,8 +505,10 @@
 		}
 	}
 
-	if (cnt->track.type != 0)	{
+	if (cnt->track.type != 0) {
+		cnt->thread_flag = 0;
 		cnt->moved = track_move(cnt, dev, &cnt->current_image->location, imgs, 0);
+		cnt->thread_flag = 1;
 	}
 }
 
@@ -589,6 +591,7 @@
 	cnt->currenttime_tm = mymalloc(sizeof(struct tm));
 	cnt->eventtime_tm = mymalloc(sizeof(struct tm));
 	cnt->smartmask_speed = 0;
+	cnt->sec_critic_section = 0;
 
 	/* We initialize cnt->event_nr to 1 and cnt->prev_event to 0 (not really needed) so
 	 * that certain code below does not run until motion has been detected the first time */
@@ -852,6 +855,8 @@
 
 	motion_init(cnt);
 
+	cnt->thread_flag = 1;
+
 	/* Initialize the double sized characters if needed. */
 	if(cnt->conf.text_double)
 		text_size_factor = 2;
@@ -1401,8 +1406,11 @@
 					/* if tracking is enabled we center our camera so it does not
 					 * point to a place where it will miss the next action
 					 */
-					if (cnt->track.type)
+					if (cnt->track.type){
+						cnt->thread_flag = 0;
 						cnt->moved = track_center(cnt, cnt->video_dev, 0, 0, 0);
+						cnt->thread_flag = 1;
+					}
 
 					if (cnt->conf.setup_mode)
 						motion_log(-1, 0, "End of event %d", cnt->event_nr);
@@ -2088,6 +2096,7 @@
 			/* Create the actual thread. Use 'motion_loop' as the thread
 			 * function.
 			 */
+			cnt_list[i]->thread_flag = 1;
 			pthread_create(&thread_id, &thread_attr, &motion_loop, cnt_list[i]);
 		}
 
@@ -2105,6 +2114,30 @@
 		 */
 		while(threads_running > 0) {
 			SLEEP(1,0);
+			for (i = 1; cnt_list[i]; i++) {
+				if ((cnt_list[i]->thread_flag < 1) && (cnt_list[i]->lost_connection != 1)) {
+					if (cnt_list[i]->thread_flag == -1) cnt_list[i]->sec_critic_section++;
+					else cnt_list[i]->thread_flag = -1;
+
+					if (cnt_list[i]->sec_critic_section > 10){
+						motion_log(LOG_DEBUG, 0, "Thread %d running hold in critic section - Restarting \n",
+								cnt_list[i]->threadnr);
+						cnt_list[i]->sec_critic_section = 0;    
+						restart = 1;
+					}
+
+					if (debug_level > 8)
+						motion_log(LOG_DEBUG, 0, "Thread %d running %d sec in critic section\n",
+									cnt_list[i]->threadnr,cnt_list[i]->sec_critic_section);
+
+					
+				}else{
+					cnt_list[i]->sec_critic_section = 0;
+					if (debug_level > 8)
+						motion_log(LOG_DEBUG, 0, "Thread %d running normal section\n",
+									cnt_list[i]->threadnr);
+				}
+			}
 		}
 
 		if (cnt_list[0]->conf.setup_mode)
Index: video.c
===================================================================
--- video.c	(revisión: 230)
+++ video.c	(copia de trabajo)
@@ -364,11 +364,16 @@
 		viddev->freq=freq;
 		viddev->tuner_number=tuner_number;
 		/* skip a few frames if needed */
-		for (i=0; i<skip; i++)
+		for (i=0; i<skip; i++) {
+			cnt->thread_flag = 0;
 			v4l_next(viddev, map, width, height);
+			cnt->thread_flag = 1;
+		}
 	} else {
 		/* No round robin - we only adjust picture controls */
+		cnt->thread_flag = 0;
 		v4l_picture_controls(cnt, viddev);
+		cnt->thread_flag = 1;
 	}
 }
 
Index: motion.h
===================================================================
--- motion.h	(revisión: 230)
+++ motion.h	(copia de trabajo)
@@ -295,6 +295,8 @@
 	char conf_filename[PATH_MAX];
 	int threadnr;
 	int daemon;
+	int thread_flag;
+	int sec_critic_section;
 
 	struct config conf;
 	struct images imgs;
Index: video2.c
===================================================================
--- video2.c	(revisión: 230)
+++ video2.c	(copia de trabajo)
@@ -761,6 +761,8 @@
 		return V4L_FATAL_ERROR;
 	}
 
+	cnt->thread_flag = 0;
+
 	/* Block signals during IOCTL */
 	sigemptyset(&set);
 	sigaddset(&set, SIGCHLD);
@@ -770,9 +772,25 @@
 	sigaddset(&set, SIGHUP);
 	pthread_sigmask(SIG_BLOCK, &set, &old);
 
+	/* Testing signal strength to detect signal lost */
+	if (viddev->input < 8){
+		struct v4l2_tuner tuner;
+
+		memset(&tuner, 0, sizeof(struct v4l2_tuner));
+		//tuner.input = 0;
+		if (xioctl(s->fd, VIDIOC_G_TUNER, &tuner) == -1) {
+			cnt->thread_flag = 1;
+			return (0);
+		}
+
+		motion_log(LOG_DEBUG, 0, "Signal strength %d\n", tuner.signal >> 14);
+	}
+
+
 	if (s->pframe >= 0) {
 		if (xioctl(s->fd, VIDIOC_QBUF, &s->buf) == -1) {
 			motion_log(LOG_ERR, 0, "%s: VIDIOC_QBUF: %s", __FUNCTION__, strerror(errno));
+			cnt->thread_flag = 1;
 			return (-1);
 		}
 	}
@@ -794,12 +812,12 @@
 			s->buf.index = s->pframe;
 
 			motion_log(LOG_ERR, 0, "%s: VIDIOC_DQBUF: EIO (s->pframe %d)", __FUNCTION__,s->pframe);
-
+			cnt->thread_flag = 1;
 			return (1);
 		}
 
 		motion_log(LOG_ERR, 0, "%s: VIDIOC_DQBUF: %s", __FUNCTION__, strerror(errno));
-
+		cnt->thread_flag = 1;
 		return (-1);
 	}
 
@@ -809,6 +827,8 @@
 
 	pthread_sigmask(SIG_UNBLOCK, &old, NULL);	/*undo the signal blocking */
 
+	cnt->thread_flag = 1;
+
 	{
 		netcam_buff *the_buffer = &s->buffers[s->buf.index];
 
Index: video_common.c
===================================================================
--- video_common.c	(revisión: 230)
+++ video_common.c	(copia de trabajo)
@@ -787,6 +787,8 @@
 	struct config *conf = &cnt->conf;
 	int dev = -1;
 
+	cnt->thread_flag = 0;
+
 	if (conf->netcam_url) {
 		return netcam_start(cnt);
 	}
@@ -794,6 +796,8 @@
 	dev = vid_v4lx_start(cnt);
 #endif				/*WITHOUT_V4L */
 
+	cnt->thread_flag = 1;
+
 	return dev;
 }
 
@@ -826,7 +830,10 @@
 		if (cnt->video_dev == -1)
 			return NETCAM_GENERAL_ERROR;
 
+		cnt->thread_flag = 0;
 		ret = netcam_next(cnt, map);
+		cnt->thread_flag = 1;
+
 		return ret;
 	}
 #ifndef WITHOUT_V4L
