Index: code/branches/netp6/src/core/CorePrereqs.h
===================================================================
--- code/branches/netp6/src/core/CorePrereqs.h	(revision 3231)
+++ code/branches/netp6/src/core/CorePrereqs.h	(revision 3240)
@@ -189,5 +189,5 @@
     // multithreading
     class Thread;
-    class ThreadGroup;
+    class ThreadPool;
 }
 
Index: code/branches/netp6/src/core/Thread.cc
===================================================================
--- code/branches/netp6/src/core/Thread.cc	(revision 3231)
+++ code/branches/netp6/src/core/Thread.cc	(revision 3240)
@@ -36,5 +36,5 @@
 
 #include "util/Sleep.h"
-#include "Functor.h"
+#include "Executor.h"
 
 namespace orxonox
@@ -44,9 +44,11 @@
     
     Thread::Thread():
-        functor_(0),
+        executor_(0),
         isWorking_(false),
         stopThread_(false)
     {
-        this->communicationMutex_ = new boost::mutex;
+        this->executorMutex_ = new boost::mutex;
+        this->isWorkingMutex_ = new boost::mutex;
+        this->stopThreadMutex_ = new boost::mutex;
         this->workerThread_ = new boost::thread( boost::bind(&Thread::threadLoop, this) );
     }
@@ -54,38 +56,60 @@
     Thread::~Thread()
     {
+        this->stopThreadMutex_->lock();
         this->stopThread_ = true;
+        this->stopThreadMutex_->unlock();
         if( !this->workerThread_->timed_join( THREAD_WAIT_BEFORE_DETACH ) )
             assert(0); // this should not happen
         delete this->workerThread_;
-        delete this->communicationMutex_;
+        delete this->executorMutex_;
+        delete this->stopThreadMutex_;
+        delete this->isWorkingMutex_;
     }
     
-    bool Thread::evaluateFunctor( Functor* functor )
+    bool Thread::isWorking()
     {
-        if( this->communicationMutex_->try_lock() )
-        {
-            this->functor_ = functor;
-            this->communicationMutex_->unlock();
-            return true;
-        }
-        else
-            return false;
+      this->isWorkingMutex_->lock();
+      bool isWorking = this->isWorking_;
+      this->isWorkingMutex_->unlock();
+      return isWorking;
+    }
+    
+    bool Thread::evaluateExecutor( Executor* executor )
+    {
+        this->isWorkingMutex_->lock();
+        this->isWorking_=true;
+        this->isWorkingMutex_->unlock();
+        this->executorMutex_->lock();
+        this->executor_ = executor;
+        this->executorMutex_->unlock();
+        return true;
     }
     
     void Thread::threadLoop()
     {
-        while( !this->stopThread_ )
+        bool stopThread = false;
+        while( !stopThread )
         {
-            this->communicationMutex_->lock();
-            if( this->functor_ )
+            this->executorMutex_->lock();
+            Executor* executor = this->executor_;
+            this->executorMutex_->unlock();
+            if( executor )
             {
-                (*this->functor_)();
-                this->communicationMutex_->unlock();
+                (*executor)();
+                this->executorMutex_->lock();
+                delete this->executor_;
+                this->executor_ = 0;
+                this->executorMutex_->unlock();
+                this->isWorkingMutex_->lock();
+                this->isWorking_=false;
+                this->isWorkingMutex_->unlock();
             }
             else
             {
-                this->communicationMutex_->unlock();
                 this->workerThread_->yield();
             }
+            this->stopThreadMutex_->lock();
+            stopThread = this->stopThread_;
+            this->stopThreadMutex_->unlock();
         }
     }
@@ -96,7 +120,7 @@
         while( stillWorking )
         {
-            this->communicationMutex_->lock();
+            this->isWorkingMutex_->lock();
             stillWorking = this->isWorking_;
-            this->communicationMutex_->unlock();
+            this->isWorkingMutex_->unlock();
             if( stillWorking )
                 msleep( 1 );
Index: code/branches/netp6/src/core/Thread.h
===================================================================
--- code/branches/netp6/src/core/Thread.h	(revision 3231)
+++ code/branches/netp6/src/core/Thread.h	(revision 3240)
@@ -32,4 +32,8 @@
 #include "CorePrereqs.h"
 
+namespace boost{
+  class recursive_mutex;
+}
+
  namespace orxonox
 {
@@ -40,16 +44,18 @@
         virtual ~Thread();
 
-        inline bool isWorking() { return this->isWorking_; }
+        bool isWorking();
         void waitUntilFinished();
-        bool evaluateFunctor( Functor* functor );
+        bool evaluateExecutor( Executor* executor );
 
     private:
         void            threadLoop();
         
-        Functor*        functor_;
+        Executor*       executor_;
         bool            isWorking_;
         bool            stopThread_;
         boost::thread*  workerThread_;
-        boost::mutex*   communicationMutex_;
+        boost::mutex*   executorMutex_;
+        boost::mutex*     isWorkingMutex_;
+        boost::mutex*   stopThreadMutex_;
     };
 
Index: code/branches/netp6/src/core/ThreadPool.cc
===================================================================
--- code/branches/netp6/src/core/ThreadPool.cc	(revision 3231)
+++ code/branches/netp6/src/core/ThreadPool.cc	(revision 3240)
@@ -28,4 +28,5 @@
 
 #include "ThreadPool.h"
+#include "Thread.h"
 #include <cassert>
 
@@ -39,4 +40,6 @@
     ThreadPool::~ThreadPool()
     {
+        unsigned int a = this->setNrOfThreads(0);
+        assert(a == 0);
     }
     
@@ -44,17 +47,21 @@
     {
         for( unsigned int i=0; i<nr; i++ )
-            this->threadPool_.push_back(Thread());
+            this->threadPool_.push_back(new Thread());
     }
     unsigned int ThreadPool::removeThreads( unsigned int nr )
     {
         unsigned int i=0;
-        std::vector<Thread>::iterator it;
-        for( it = this->threadPool_.begin(); it != threadPool_.end() && i<nr; ++it )
+        std::vector<Thread*>::iterator it;
+        for( it = this->threadPool_.begin(); it != threadPool_.end() && i<nr; )
         {
-            if( ! it->isWorking() )
+            if( ! (*it)->isWorking() )
             {
-                this->threadPool_.erase( it++ );
+                Thread* temp = *it;
+                it=this->threadPool_.erase( it );
+                delete temp;
                 ++i;
             }
+            else
+              ++it;
         }
         return i;
@@ -74,12 +81,12 @@
     }
     
-    bool ThreadPool::passFunction( Functor* functor, bool addThread )
+    bool ThreadPool::passFunction( Executor* executor, bool addThread )
     {
-        std::vector<Thread>::iterator it;
+        std::vector<Thread*>::iterator it;
         for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it )
         {
-            if ( ! it->isWorking() )
+            if ( ! (*it)->isWorking() )
             {
-                bool b = it->evaluateFunctor( functor );
+                bool b = (*it)->evaluateExecutor( executor );
                 assert(b); // if b is false then there is some code error
                 return true;
@@ -89,5 +96,6 @@
         {
             addThreads( 1 );
-            this->threadPool_.back().evaluateFunctor( functor ); // access the last element
+            bool b = this->threadPool_.back()->evaluateExecutor( executor ); // access the last element
+            assert(b);
             return true;
         }
@@ -98,8 +106,8 @@
     void ThreadPool::synchronise()
     {
-        std::vector<Thread>::iterator it;
+        std::vector<Thread*>::iterator it;
         for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it )
         {
-            it->waitUntilFinished();
+            (*it)->waitUntilFinished();
         }
     }
Index: code/branches/netp6/src/core/ThreadPool.h
===================================================================
--- code/branches/netp6/src/core/ThreadPool.h	(revision 3231)
+++ code/branches/netp6/src/core/ThreadPool.h	(revision 3240)
@@ -33,5 +33,4 @@
 
 #include <vector>
-#include "Thread.h"
 
  namespace orxonox
@@ -47,9 +46,9 @@
         unsigned int setNrOfThreads( unsigned int nr );
         
-        bool passFunction( Functor* functor, bool addThread=false );
+        bool passFunction( Executor* executor, bool addThread=false );
         void synchronise();
         
     private:
-        std::vector<Thread> threadPool_;
+        std::vector<Thread*> threadPool_;
         
     };
