runnable.h 4.87 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
31
#ifndef RUNNABLE_H__
#define RUNNABLE_H__

32
#include "cgt/logmanager.h"
33
34

#include <ext/threading.h>
35
#include <tbb/atomic.h>
36

37
namespace cgt {
38
39
    class RunnableWithConditionalWait;

40
41
    /**
     * Abstract base class for objects that shall run in a separate thread.
42
43
44
     * 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.
45
     */
46
    class CGT_API Runnable {
47
48
49
        friend void invokeThread(Runnable* r);
        friend class RunnableWithConditionalWait;

50
    public:
51
52
53
54
55
56
        /**
         * Creates a new Runnable object
         */
        Runnable();

        /**
57
         * Destructor, stops and waits for the thread to finish if the thread is still running.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
         */
        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;
76
77

    protected:
78
        tbb::atomic<bool> _stopExecution;       ///< Flag whether the thread should stop
79
80

    private:
81
        /// Runnables are not copyable
82
        explicit Runnable(Runnable const&);
83
84
        /// Runnables are not copyable
        Runnable& operator =(Runnable const&);
85

schultezub's avatar
schultezub committed
86
        std::thread* _thread;                    ///< Thread of the Runnable
87
        tbb::atomic<bool> _running;
88
89
    };

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120


    /**
     * Extension of the Runnable interface for threads that should use conditional wait to pause
     * their work when there's nothing to do.
     * This version adds a protected std::condition_variable to be used for conditional wait and
     * further overloads the stop() method to cleanly halt the thread by repeatedly notifying
     * the condition variable (as notifications may get lost due to race conditions).
     */
    class CGT_API RunnableWithConditionalWait : public Runnable {
    public:
        /**
         * Creates a new RunnableWithConditionalWait object
         */
        RunnableWithConditionalWait();

        /**
         * Destructor, stops and waits for thread to finish if the thread is still running.
         */
        virtual ~RunnableWithConditionalWait();

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

    protected:
        /// conditional wait to be used when there are currently no jobs to process
        std::condition_variable _evaluationCondition;
    };

121
122
123
}

#endif // RUNNABLE_H__