ThreadBase.h
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: ThreadBase.h
37 // Author: Pedram Azad
38 // Date: 2004
39 // ****************************************************************************
40 
41 
42 #ifndef _THREAD_BASE_H_
43 #define _THREAD_BASE_H_
44 
45 
46 // ****************************************************************************
47 // Necessary includes
48 // ****************************************************************************
49 
50 #include "Helpers/helpers.h"
51 #include <stdio.h>
52 
53 
54 // ****************************************************************************
55 // Typedefs
56 // ****************************************************************************
57 
58 typedef int (*ThreadMethodType)(void *pParameter);
59 
60 
61 
62 // ****************************************************************************
63 // CThreadBase
64 // ****************************************************************************
65 
67 {
68 public:
69  // constructor
71  {
72  m_bExit = true;
73  m_pThreadMethod = 0;
74  }
75 
76  // destructor
77  virtual ~CThreadBase() { }
78 
79 
80  // public methods
81  void Start(void *pParameter, ThreadMethodType pThreadMethod, int nKillThreadTimeout = 5000)
82  {
83  Stop();
84 
85  m_bExit = false;
86  m_pParameter = pParameter;
87  m_pThreadMethod = pThreadMethod;
88  m_nKillThreadTimeout = nKillThreadTimeout;
89  m_bCompletelyDone = false;
90  _Start();
91  }
92 
93  void Start(int nKillThreadTimeout = 5000)
94  {
95  Stop();
96 
97  m_bExit = false;
98  m_pParameter = 0;
99  m_pThreadMethod = 0;
100  m_nKillThreadTimeout = nKillThreadTimeout;
101  m_bCompletelyDone = false;
102  _Start();
103  }
104 
105  void Stop()
106  {
107  if (!IsRunning())
108  return;
109 
110  m_bExit = true;
111 
113 
114  _Stop();
115 
116  for (int i = 0; i < 100; i++)
117  {
118  if (m_bCompletelyDone)
119  break;
120 
121  sleep_ms(1);
122  }
123  }
124 
125  bool IsRunning() { return !m_bExit; }
126  bool GetExit() { return m_bExit; }
127 
128 
129 protected:
130  // virtual thread method: can be implemented if C++ style is desired
131  virtual int ThreadMethod()
132  {
133  if (!m_pThreadMethod)
134  {
135  printf("error: thread function not implemented, but using thread function variant.\n");
136  return -1;
137  }
138 
140  }
141 
142  // use this if you need a callback after stop has been called and m_bExit set to true
143  virtual void StopThreadCallback() { }
144 
145  virtual void _Start() = 0;
146  virtual void _Stop() = 0;
147 
149 
150 
151 private:
152  // private methods
153  virtual void ThreadMethodFinished() { }
154 
155  // private attributes
158  bool m_bExit;
159 
160 
161 public:
162  // thread function access
164  {
165  const int nRet = ThreadMethod();
166 
168 
169  return nRet;
170  }
171 
173 };
174 
175 
176 
177 #endif /* _THREAD_BASE_H_ */
int _ThreadMethod()
Definition: ThreadBase.h:163
void Start(void *pParameter, ThreadMethodType pThreadMethod, int nKillThreadTimeout=5000)
Definition: ThreadBase.h:81
bool m_bExit
Definition: ThreadBase.h:158
void * m_pParameter
Definition: ThreadBase.h:157
void Start(int nKillThreadTimeout=5000)
Definition: ThreadBase.h:93
virtual void _Stop()=0
virtual int ThreadMethod()
Definition: ThreadBase.h:131
virtual void StopThreadCallback()
Definition: ThreadBase.h:143
int m_nKillThreadTimeout
Definition: ThreadBase.h:148
void sleep_ms(unsigned int ms)
Definition: helpers.cpp:336
virtual void ThreadMethodFinished()
Definition: ThreadBase.h:153
ThreadMethodType m_pThreadMethod
Definition: ThreadBase.h:156
bool IsRunning()
Definition: ThreadBase.h:125
int(* ThreadMethodType)(void *pParameter)
Definition: ThreadBase.h:58
void Stop()
Definition: ThreadBase.h:105
bool m_bCompletelyDone
Definition: ThreadBase.h:172
virtual ~CThreadBase()
Definition: ThreadBase.h:77
virtual void _Start()=0
bool GetExit()
Definition: ThreadBase.h:126


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28