Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/util/Thread.cc @ 3226

Last change on this file since 3226 was 3226, checked in by scheusso, 15 years ago

added 2 classes:
Thread: implementation of a worker thread (you can pass (threadsafe) functions to it by wrapping them into a functor)
ThreadPool: pool of Threads. dispatches work (functors) to a thread with no work and coordinates the finishing of all threads (ideal for parallel work and synchronisation of the threads afterwards)

File size: 2.8 KB
RevLine 
[3226]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Thread.h"
30
31#include <cassert>
32#include <boost/thread/thread.hpp>
33#include <boost/bind.hpp>
34#include <boost/thread/mutex.hpp>
35#include <boost/thread/thread_time.hpp>
36
37#include "core/Functor.h"
38#include "util/Sleep.h"
39
40namespace orxonox
41{
42    boost::posix_time::millisec THREAD_WAIT_BEFORE_DETACH(1000);
43   
44   
45    Thread::Thread():
46        functor_(0),
47        isWorking_(false),
48        stopThread_(false)
49    {
50        this->communicationMutex_ = new boost::mutex;
51        this->workerThread_ = new boost::thread( boost::bind(&Thread::threadLoop, this) );
52    }
53   
54    Thread::~Thread()
55    {
56        this->stopThread_ = true;
57        if( !this->workerThread_->timed_join( THREAD_WAIT_BEFORE_DETACH ) )
58            assert(0); // this should not happen
59        delete this->workerThread_;
60        delete this->communicationMutex_;
61    }
62   
63    bool Thread::evaluateFunctor( Functor* functor )
64    {
65        if( this->communicationMutex_->try_lock() )
66        {
67            this->functor_ = functor;
68            this->communicationMutex_->unlock();
69            return true;
70        }
71        else
72            return false;
73    }
74   
75    void Thread::threadLoop()
76    {
77        while( !this->stopThread_ )
78        {
79            this->communicationMutex_->lock();
80            if( this->functor_ )
81            {
82                (*this->functor_)();
83                this->communicationMutex_->unlock();
84            }
85            else
86            {
87                this->communicationMutex_->unlock();
88                this->workerThread_->yield();
89            }
90        }
91    }
92   
93    void Thread::waitUntilFinished()
94    {
95        bool stillWorking = true;
96        while( stillWorking )
97        {
98            this->communicationMutex_->lock();
99            stillWorking = this->isWorking_;
100            this->communicationMutex_->unlock();
101            if( stillWorking )
102                msleep( 1 );
103        }
104    }
105}
Note: See TracBrowser for help on using the repository browser.