Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialHS09/src/libraries/core/ThreadPool.cc @ 12373

Last change on this file since 12373 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
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 "ThreadPool.h"
30#include "Thread.h"
31#include <cassert>
32
33namespace orxonox
34{
35
36    ThreadPool::ThreadPool()
37    {
38    }
39   
40    ThreadPool::~ThreadPool()
41    {
42        unsigned int a = this->setNrOfThreads(0);
43        assert(a == 0);
44    }
45   
46    void ThreadPool::addThreads( unsigned int nr )
47    {
48        for( unsigned int i=0; i<nr; i++ )
49            this->threadPool_.push_back(new Thread());
50    }
51    unsigned int ThreadPool::removeThreads( unsigned int nr )
52    {
53        unsigned int i=0;
54        std::vector<Thread*>::iterator it;
55        for( it = this->threadPool_.begin(); it != threadPool_.end() && i<nr; )
56        {
57            if( ! (*it)->isWorking() )
58            {
59                Thread* temp = *it;
60                it=this->threadPool_.erase( it );
61                delete temp;
62                ++i;
63            }
64            else
65              ++it;
66        }
67        return i;
68    }
69    unsigned int ThreadPool::setNrOfThreads( unsigned int nr )
70    {
71        unsigned int currentsize = this->threadPool_.size();
72        if ( nr < currentsize )
73            return currentsize - removeThreads( currentsize - nr );
74        else if ( nr == currentsize )
75            return currentsize;
76        else
77        {
78            addThreads( nr - currentsize );
79            return nr;
80        }
81    }
82   
83    bool ThreadPool::passFunction( Executor* executor, bool addThread )
84    {
85        std::vector<Thread*>::iterator it;
86        for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it )
87        {
88            if ( ! (*it)->isWorking() )
89            {
90                bool b = (*it)->evaluateExecutor( executor );
91                assert(b); // if b is false then there is some code error
92                return true;
93            }
94        }
95        if ( addThread )
96        {
97            addThreads( 1 );
98            bool b = this->threadPool_.back()->evaluateExecutor( executor ); // access the last element
99            assert(b);
100            return true;
101        }
102        else
103            return false;
104    }
105   
106    void ThreadPool::synchronise()
107    {
108        std::vector<Thread*>::iterator it;
109        for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it )
110        {
111            (*it)->waitUntilFinished();
112        }
113    }
114
115}
Note: See TracBrowser for help on using the repository browser.