runnable.h 4.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**********************************************************************
 *                                                                    *
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2014               *
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
 *                                                                    *
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
 *                                                                    *
 * This file is part of the cgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

29
30
// ================================================================================================
// 
schultezub's avatar
schultezub committed
31
// This file is part of the CAMPVis Software Framework.
32
// 
33
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
34
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
35
//      Chair for Computer Aided Medical Procedures
36
37
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
38
// 
schultezub's avatar
schultezub committed
39
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
40
// 
41
42
43
44
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
45
// 
46
47
48
49
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
50
51
52
// 
// ================================================================================================

53
54
55
#ifndef RUNNABLE_H__
#define RUNNABLE_H__

56
#include "cgt/logmanager.h"
57
58

#include <ext/threading.h>
59
#include <tbb/atomic.h>
60

61
namespace cgt {
62
63
    /**
     * Abstract base class for objects that shall run in a separate thread.
64
65
66
     * Runnable object manage their own thread, which is created when calling start(). The new Thread starts
     * in the run() method which has to be overwritten in subclasses. Calling stop() sets _stopExecution to true
     * and waits for the thread to finish. Hence, you should test for _stopExecution in your run() method.
67
     */
68
    class CGT_API Runnable {
69
    public:
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        /**
         * Creates a new Runnable object
         */
        Runnable();

        /**
         * Destructor, stops and waits the thread if the thread is still running.
         */
        virtual ~Runnable();

        /**
         * Creates the new thread evaluating the run() method.
         * \sa  Runnable::run
         */
        virtual void start();

        /**
         * Sets the _stopExecution flag and waits for the thread to finish.
         */
        virtual void stop();

        /**
         * Entrance point for the new thread. To be overwritten in subclasses.
         */
        virtual void run() = 0;
95
96

    protected:
97
        tbb::atomic<bool> _stopExecution;       ///< Flag whether the thread should stop
98
99

    private:
100
101
102
103
        /// Runnables are not copyable
        Runnable(Runnable const&);
        /// Runnables are not copyable
        Runnable& operator =(Runnable const&);
104

schultezub's avatar
schultezub committed
105
        std::thread* _thread;                    ///< Thread of the Runnable
106
        tbb::atomic<bool> _running;
107
108
109
110
111
    };

}

#endif // RUNNABLE_H__