ThreadBase.h
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  ThreadBase.h
00037 // Author:    Pedram Azad
00038 // Date:      2004
00039 // ****************************************************************************
00040 
00041 
00042 #ifndef _THREAD_BASE_H_
00043 #define _THREAD_BASE_H_
00044 
00045 
00046 // ****************************************************************************
00047 // Necessary includes
00048 // ****************************************************************************
00049 
00050 #include "Helpers/helpers.h"
00051 #include <stdio.h>
00052 
00053 
00054 // ****************************************************************************
00055 // Typedefs
00056 // ****************************************************************************
00057 
00058 typedef int (*ThreadMethodType)(void *pParameter);
00059  
00060 
00061 
00062 // ****************************************************************************
00063 // CThreadBase
00064 // ****************************************************************************
00065 
00066 class CThreadBase
00067 {
00068 public:
00069         // constructor
00070         CThreadBase()
00071         {
00072                 m_bExit = true;
00073                 m_pThreadMethod = 0;
00074         }
00075 
00076         // destructor
00077         virtual ~CThreadBase() { }
00078         
00079         
00080         // public methods
00081         void Start(void *pParameter, ThreadMethodType pThreadMethod, int nKillThreadTimeout = 5000)
00082         {
00083                 Stop();
00084 
00085                 m_bExit = false;
00086                 m_pParameter = pParameter;
00087                 m_pThreadMethod = pThreadMethod;
00088                 m_nKillThreadTimeout = nKillThreadTimeout;
00089                 m_bCompletelyDone = false;
00090                 _Start();
00091         }
00092         
00093         void Start(int nKillThreadTimeout = 5000)
00094         {
00095                 Stop();
00096 
00097                 m_bExit = false;
00098                 m_pParameter = 0;
00099                 m_pThreadMethod = 0;
00100                 m_nKillThreadTimeout = nKillThreadTimeout;
00101                 m_bCompletelyDone = false;
00102                 _Start();
00103         }
00104 
00105         void Stop()
00106         {
00107                 if (!IsRunning())
00108                         return;
00109 
00110                 m_bExit = true;
00111         
00112                 StopThreadCallback();
00113 
00114                 _Stop();
00115 
00116                 for (int i = 0; i < 100; i++)
00117                 {
00118                         if (m_bCompletelyDone)
00119                                 break;
00120 
00121                         sleep_ms(1);
00122                 }
00123         }
00124 
00125         bool IsRunning() { return !m_bExit; }
00126         bool GetExit() { return m_bExit; }
00127 
00128         
00129 protected:
00130         // virtual thread method: can be implemented if C++ style is desired
00131         virtual int ThreadMethod()
00132         {
00133                 if (!m_pThreadMethod)
00134                 {
00135                         printf("error: thread function not implemented, but using thread function variant.\n");
00136                         return -1;
00137                 }
00138                 
00139                 return m_pThreadMethod(m_pParameter);
00140         }
00141         
00142         // use this if you need a callback after stop has been called and m_bExit set to true
00143         virtual void StopThreadCallback() { }
00144 
00145         virtual void _Start() = 0;
00146         virtual void _Stop() = 0;
00147 
00148         int m_nKillThreadTimeout;
00149 
00150 
00151 private:
00152         // private methods
00153         virtual void ThreadMethodFinished() { }
00154 
00155         // private attributes
00156         ThreadMethodType m_pThreadMethod;
00157         void *m_pParameter;
00158         bool m_bExit;
00159         
00160 
00161 public:
00162         // thread function access
00163         int _ThreadMethod()
00164         {
00165                 const int nRet = ThreadMethod();
00166                 
00167                 ThreadMethodFinished();
00168 
00169                 return nRet;
00170         }
00171 
00172         bool m_bCompletelyDone;
00173 };
00174 
00175 
00176 
00177 #endif /* _THREAD_BASE_H_ */


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:58