version 0.1.11
[fms.git] / src / pthreadwrapper / thread.cpp
index 7a4e972..07cb689 100644 (file)
@@ -9,12 +9,36 @@
 namespace PThread\r
 {\r
 \r
+void Sleep(const long ms)\r
+{\r
+       pthread_cond_t c;\r
+       pthread_mutex_t m;\r
+       timespec t;\r
+       timeb tb;\r
+\r
+       pthread_mutex_init(&m,NULL);\r
+       pthread_cond_init(&c,NULL);\r
+\r
+       ftime(&tb);\r
+\r
+       t.tv_sec=tb.time+(ms/1000);\r
+       t.tv_nsec=((1000000L)*(long)tb.millitm)+((1000000L)*(ms%1000));\r
+\r
+       pthread_mutex_lock(&m);\r
+       pthread_cond_timedwait(&c,&m,&t);\r
+       pthread_mutex_unlock(&m);\r
+\r
+       pthread_cond_destroy(&c);\r
+       pthread_mutex_destroy(&m);\r
+}\r
+\r
 Thread::Thread()\r
 {\r
        m_running=false;\r
        m_cancelled=false;\r
        m_runnable=0;\r
        m_threadnum=0;\r
+       m_threadcleaned=true;\r
 }\r
 \r
 Thread::Thread(Runnable *runnable)\r
@@ -23,6 +47,7 @@ Thread::Thread(Runnable *runnable)
        m_cancelled=false;\r
        m_runnable=runnable;\r
        m_threadnum=0;\r
+       m_threadcleaned=true;\r
        if(m_runnable)\r
        {\r
                m_runnable->m_thread=this;\r
@@ -34,6 +59,10 @@ Thread::~Thread()
 {\r
        Cancel();\r
        Join();\r
+       if(m_threadcleaned==false)\r
+       {\r
+               pthread_detach(m_thread);\r
+       }\r
        if(m_runnable)\r
        {\r
                delete m_runnable;\r
@@ -65,6 +94,7 @@ void Thread::Join()
        if(m_running)\r
        {\r
                pthread_join(m_thread,NULL);\r
+               m_threadcleaned=true;\r
        }\r
 }\r
 \r
@@ -88,12 +118,16 @@ void Thread::Sleep(const long ms)
                pthread_mutex_lock(&m);\r
                pthread_cond_timedwait(&c,&m,&t);\r
                pthread_mutex_unlock(&m);\r
+\r
+               pthread_cond_destroy(&c);\r
+               pthread_mutex_destroy(&m);\r
        }\r
 }\r
 \r
 void Thread::Start()\r
 {\r
        m_running=true;\r
+       m_threadcleaned=false;\r
        m_threadnum=pthread_create(&m_thread,NULL,Thread::EntryPoint,this);\r
 }\r
 \r