Image Component Library (ICL)
Thread.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Thread.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
35 #include <ICLUtils/Mutex.h>
36 
37 namespace icl{
38  namespace utils{
39 
41  class ICLUtils_API ThreadImpl;
42  struct ICLUtils_API ThreadImplDelOp{
43  static void delete_func( ThreadImpl* impl );
44  };
49 
90  class ICLUtils_API Thread : public ShallowCopyable<ThreadImpl, ThreadImplDelOp>{
91  public:
92 
94  Thread();
95 
97  virtual ~Thread();
98 
100 
101  void start();
102 
104  virtual void stop();
105 
107 
110  void wait();
111 
113  virtual void run()=0;
114 
116  virtual void finalize(){}
117 
118 
120  static void usleep(unsigned int usec);
121 
123 
124  static void msleep(unsigned int msecs);
125 
127 
128  static void sleep(float secs);
129 
131 
133  bool running() const;
134 
136 
138  bool runningNoLock() const;
139 
140  protected:
141 
143  void exit();
144 
146 
151  void lock();
153 
156  int trylock();
157 
159  void unlock();
160 
162  void join();
163  };
164 
165 
167 
173  template<class T>
174  static inline void saveDelete(T* &pointer){
175  static Mutex m;
176  m.lock();
177  ICL_DELETE(pointer);
178  m.unlock();
179  }
181 
186  template<class T, void (T::*func)()>
187  static inline void saveCall(T *obj){
188  static Mutex m;
189  m.lock();
190  (obj->*func)();
191  m.unlock();
192  }
193  } // namespace utils
194 }
195 
196 
undocument this line if you encounter any issues!
Definition: Any.h:37
void unlock()
unlocks the mutex
Definition: Mutex.h:111
Simple object oriented thread class wrapping the pthread library.
Definition: Thread.h:90
Interface class for cheap copyable classes using a smart ptr.
Definition: ShallowCopyable.h:131
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
static void saveDelete(T *&pointer)
static utility function which deletes a pointer and sets it to NULL
Definition: Thread.h:174
static void saveCall(T *obj)
static utility function which ensures Thread-safety for object functions
Definition: Thread.h:187
#define ICL_DELETE(X)
Definition: Macros.h:242
Mutex class of the ICL.
Definition: Mutex.h:54
void lock()
locks the mutex
Definition: Mutex.h:91
virtual void finalize()
at the end of the stop function, this function is called
Definition: Thread.h:116