Browse Source

whait for running threads to exit when the framework is destructed

Kolja Strohm 2 years ago
parent
commit
44600c1d62
3 changed files with 20 additions and 4 deletions
  1. 2 2
      Array.h
  2. 17 2
      Thread.cpp
  3. 1 0
      Thread.h

+ 2 - 2
Array.h

@@ -268,8 +268,8 @@ namespace Framework
             }
             last->next = new ArrayEintrag< TYP >();
             last = last->next;
-            last->set = 0;
-            last->next = 0;
+            last->set = 1;
+            last->var = t;
         }
 
         //! Fügt ein Element bei einer bestimmten Position in die Liste ein

+ 17 - 2
Thread.cpp

@@ -118,10 +118,16 @@ int Thread::warteAufThread( int zeit ) // wartet zeit lang auf den Thread
         return 0;
     if( pthread_self() == threadHandle )
         return 0;
+    timespec ts;
+    ts.tv_sec = 0;
+    ts.tv_nsec = 1000 * zeit;
     if( threadHandleSys )
         *threadHandleSys = 0;
-    int ret = pthread_join( threadHandle, 0 );
-    threadHandle = 0;
+    int ret = pthread_timedjoin_np( threadHandle, 0, &ts );
+    if( !ret )
+        threadHandle = 0;
+    else
+        *threadHandleSys = threadHandle;
     return ret;
 #endif
 }
@@ -186,6 +192,15 @@ ThreadRegister::ThreadRegister()
 // Destruktor
 ThreadRegister::~ThreadRegister()
 {
+    EnterCriticalSection( &cs );
+    while( threads.hat( 0 ) )
+    {
+        LeaveCriticalSection( &cs );
+        Sleep( 1000 ); // wait until all threads are done
+        EnterCriticalSection( &cs );
+    }
+    cleanUpClosedThreads();
+    LeaveCriticalSection( &cs );
     DeleteCriticalSection( &cs );
 }
 

+ 1 - 0
Thread.h

@@ -3,6 +3,7 @@
 
 #include "Array.h"
 #include "ReferenceCounter.h"
+#include <condition_variable>
 
 namespace Framework
 {