Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
IVT
src
Threading
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
66
class
CThreadBase
67
{
68
public
:
69
// constructor
70
CThreadBase
()
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
112
StopThreadCallback
();
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
139
return
m_pThreadMethod
(
m_pParameter
);
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
148
int
m_nKillThreadTimeout
;
149
150
151
private
:
152
// private methods
153
virtual
void
ThreadMethodFinished
() { }
154
155
// private attributes
156
ThreadMethodType
m_pThreadMethod
;
157
void
*
m_pParameter
;
158
bool
m_bExit
;
159
160
161
public
:
162
// thread function access
163
int
_ThreadMethod
()
164
{
165
const
int
nRet =
ThreadMethod
();
166
167
ThreadMethodFinished
();
168
169
return
nRet;
170
}
171
172
bool
m_bCompletelyDone
;
173
};
174
175
176
177
#endif
/* _THREAD_BASE_H_ */
CThreadBase::_ThreadMethod
int _ThreadMethod()
Definition:
ThreadBase.h:163
CThreadBase::Start
void Start(void *pParameter, ThreadMethodType pThreadMethod, int nKillThreadTimeout=5000)
Definition:
ThreadBase.h:81
CThreadBase::m_bExit
bool m_bExit
Definition:
ThreadBase.h:158
CThreadBase::m_pParameter
void * m_pParameter
Definition:
ThreadBase.h:157
CThreadBase::Start
void Start(int nKillThreadTimeout=5000)
Definition:
ThreadBase.h:93
CThreadBase::CThreadBase
CThreadBase()
Definition:
ThreadBase.h:70
CThreadBase::_Stop
virtual void _Stop()=0
CThreadBase::ThreadMethod
virtual int ThreadMethod()
Definition:
ThreadBase.h:131
CThreadBase::StopThreadCallback
virtual void StopThreadCallback()
Definition:
ThreadBase.h:143
CThreadBase::m_nKillThreadTimeout
int m_nKillThreadTimeout
Definition:
ThreadBase.h:148
sleep_ms
void sleep_ms(unsigned int ms)
Definition:
helpers.cpp:336
CThreadBase::ThreadMethodFinished
virtual void ThreadMethodFinished()
Definition:
ThreadBase.h:153
CThreadBase::m_pThreadMethod
ThreadMethodType m_pThreadMethod
Definition:
ThreadBase.h:156
CThreadBase::IsRunning
bool IsRunning()
Definition:
ThreadBase.h:125
ThreadMethodType
int(* ThreadMethodType)(void *pParameter)
Definition:
ThreadBase.h:58
CThreadBase::Stop
void Stop()
Definition:
ThreadBase.h:105
CThreadBase::m_bCompletelyDone
bool m_bCompletelyDone
Definition:
ThreadBase.h:172
helpers.h
CThreadBase
Definition:
ThreadBase.h:66
CThreadBase::~CThreadBase
virtual ~CThreadBase()
Definition:
ThreadBase.h:77
CThreadBase::_Start
virtual void _Start()=0
CThreadBase::GetExit
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