--- /dev/null
+From 3c7cbd685017c1bf9ba2eaa811b63842bec28f64 Mon Sep 17 00:00:00 2001
+From: Mr-DaveDev <MotionMrDaveDev@gmail.com>
+Date: Mon, 1 Jan 2018 13:07:08 -0700
+Subject: [PATCH] Initialize the thread at start of main
+
+Closes #589
+---
+ logger.c | 5 -----
+ motion.c | 30 ++++++++++++++++--------------
+ 2 files changed, 16 insertions(+), 19 deletions(-)
+
+diff --git a/logger.c b/logger.c
+index c55044b..5ef2f85 100644
+--- a/logger.c
++++ b/logger.c
+@@ -193,11 +193,6 @@ void motion_log(int level, unsigned int type, int errno_flag, const char *fmt, .
+
+ //printf("log_type %d, type %d level %d\n", log_type, type, level);
+
+- /*
+- * If pthread_getspecific fails (e.g., because the thread's TLS doesn't
+- * contain anything for thread number, it returns NULL which casts to zero,
+- * which is nice because that's what we want in that case.
+- */
+ threadnr = (unsigned long)pthread_getspecific(tls_key_threadnr);
+
+ /*
+diff --git a/motion.c b/motion.c
+index 985d4b2..9fe58c1 100644
+--- a/motion.c
++++ b/motion.c
+@@ -2886,10 +2886,6 @@ static void motion_startup(int daemonize, int argc, char *argv[])
+ }
+
+
+- //set_log_level(cnt_list[0]->log_level);
+-
+- MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, "Motion "VERSION" Started");
+-
+ if ((cnt_list[0]->conf.log_file) && (strncmp(cnt_list[0]->conf.log_file, "syslog", 6))) {
+ set_log_mode(LOGMODE_FILE);
+ ptr_logfile = set_logfile(cnt_list[0]->conf.log_file);
+@@ -2908,6 +2904,8 @@ static void motion_startup(int daemonize, int argc, char *argv[])
+ MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, "Logging to syslog");
+ }
+
++ MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, "Motion "VERSION" Started");
++
+ if ((cnt_list[0]->conf.log_type_str == NULL) ||
+ !(cnt_list[0]->log_type = get_log_type(cnt_list[0]->conf.log_type_str))) {
+ cnt_list[0]->log_type = TYPE_DEFAULT;
+@@ -3053,8 +3051,22 @@ int main (int argc, char **argv)
+ */
+ struct sigaction sig_handler_action;
+ struct sigaction sigchild_action;
++
++
+ setup_signals(&sig_handler_action, &sigchild_action);
+
++ /*
++ * Create and a thread attribute for the threads we spawn later on.
++ * PTHREAD_CREATE_DETACHED means to create threads detached, i.e.
++ * their termination cannot be synchronized through 'pthread_join'.
++ */
++ pthread_attr_init(&thread_attr);
++ pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
++
++ /* Create the TLS key for thread number. */
++ pthread_key_create(&tls_key_threadnr, NULL);
++ pthread_setspecific(tls_key_threadnr, (void *)(0));
++
+ motion_startup(1, argc, argv);
+
+ ffmpeg_global_init();
+@@ -3102,16 +3114,6 @@ int main (int argc, char **argv)
+ if (cnt_list[0]->conf.setup_mode)
+ MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, "Motion running in setup mode.");
+
+- /*
+- * Create and a thread attribute for the threads we spawn later on.
+- * PTHREAD_CREATE_DETACHED means to create threads detached, i.e.
+- * their termination cannot be synchronized through 'pthread_join'.
+- */
+- pthread_attr_init(&thread_attr);
+- pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+-
+- /* Create the TLS key for thread number. */
+- pthread_key_create(&tls_key_threadnr, NULL);
+
+ do {
+ if (restart) {
+--
+2.7.4
+