From 910feb0a07340f986387433d0c1f75840b317bd0 Mon Sep 17 00:00:00 2001
From: Thulinma <jaron@vietors.com>
Date: Wed, 11 Jun 2014 11:05:37 +0200
Subject: [PATCH] Fixed tinythread not supporting deleting detached thread
 objects.

---
 lib/tinythread.cpp | 12 ++++++++++--
 lib/tinythread.h   |  1 +
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/lib/tinythread.cpp b/lib/tinythread.cpp
index 690eceea..98421ea4 100644
--- a/lib/tinythread.cpp
+++ b/lib/tinythread.cpp
@@ -176,8 +176,11 @@ void * thread::wrapper_function(void * aArg)
   }
 
   // The thread is no longer executing
-  lock_guard<mutex> guard(ti->mThread->mDataMutex);
-  ti->mThread->mNotAThread = true;
+  if (ti->mThread){
+    lock_guard<mutex> guard(ti->mThread->mDataMutex);
+    ti->mThread->mNotAThread = true;
+    ti->mThread->ti_copy = 0;
+  }
 
   // The thread is responsible for freeing the startup information
   delete ti;
@@ -193,6 +196,7 @@ thread::thread(void (*aFunction)(void *), void * aArg)
   // Fill out the thread startup information (passed to the thread wrapper,
   // which will eventually free it)
   _thread_start_info * ti = new _thread_start_info;
+  ti_copy = ti;
   ti->mFunction = aFunction;
   ti->mArg = aArg;
   ti->mThread = this;
@@ -212,12 +216,16 @@ thread::thread(void (*aFunction)(void *), void * aArg)
   if(!mHandle)
   {
     mNotAThread = true;
+    ti_copy = 0;
     delete ti;
   }
 }
 
 thread::~thread()
 {
+  if (ti_copy){
+    ((_thread_start_info *)ti_copy)->mThread = 0;
+  }
   if(joinable())
     std::terminate();
 }
diff --git a/lib/tinythread.h b/lib/tinythread.h
index aed7b585..e3902dd3 100644
--- a/lib/tinythread.h
+++ b/lib/tinythread.h
@@ -554,6 +554,7 @@ class thread {
     native_handle_type mHandle;   ///< Thread handle.
     mutable mutex mDataMutex;     ///< Serializer for access to the thread private data.
     bool mNotAThread;             ///< True if this object is not a thread of execution.
+    void * ti_copy;
 #if defined(_TTHREAD_WIN32_)
     unsigned int mWin32ThreadID;  ///< Unique thread ID (filled out by _beginthreadex).
 #endif