SickThread.hpp
Go to the documentation of this file.
1 //
2 // SickThread.hpp
3 //
4 
5 #ifndef SICKTHREAD_HPP
6 #define SICKTHREAD_HPP
7 
9 #include <pthread.h>
10 #include <unistd.h>
11 
12 
13 extern "C" void* wrapper_prerun(void*);
15 {
16  pthread_t t_id;
17  friend void* wrapper_prerun(void*);
18  virtual void thread_entry() = 0;
19  protected:
20  void* pthis;
21  public:
22 
23  ThreadWrapperBase() {pthis = NULL;};
24  virtual ~ThreadWrapperBase() {};
25 
26  void run(void* classptr)
27  {
28  if (pthis == NULL)
29  {
30  pthis = classptr;
31  pthread_create(&t_id, NULL, wrapper_prerun, this);
32  }
33  }
34 
35  bool isRunning()
36  {
37  if (pthis == NULL)
38  {
39  return false;
40  }
41 
42  return true;
43  }
44 
45  void join()
46  {
47  pthread_join(t_id, NULL);
48  pthis = NULL;
49  }
50 
51  pthread_t* get_thread_id()
52  {
53  return &t_id;
54  }
55 };
56 
57 
59 
79 template <typename T, void (T::*M)(bool&, UINT16&)>
81 {
82  void thread_entry()
83  {
84  T* pt = static_cast<T*>(pthis);
85 
86  m_threadShouldRun = true;
87  bool endThread = false;
88  UINT16 sleepTimeMs = 0;
89 
90  while ((m_threadShouldRun == true) && (endThread == false))
91  {
92  usleep(((UINT32)sleepTimeMs) * 1000);
93  (pt->*M)(endThread, sleepTimeMs);
94  }
95  }
96 
97 
98 public:
99  void join()
100  {
101  m_threadShouldRun = false;
103  }
104 
105  SickThread(){m_threadShouldRun = true;}
106  virtual ~SickThread(){};
107  bool m_threadShouldRun;
108 };
109 
110 /*
111 template <typename T, void (T::*M)()>
112 class SickThread : public ThreadWrapperBase
113 {
114  void thread_entry()
115  {
116  T* pt = static_cast<T*>(pthis);
117 
118 
119  (pt->*M)();
120  }
121 public:
122  SickThread(){}
123  virtual ~SickThread(){};
124 };
125 */
126 
127 
128 
129 // class SickThread
130 // {
131 // public:
132 // /**
133 // * The thread callback function.
134 // *
135 // * \param endThisThread A bool flag that may be set by the callback function
136 // * to "false" in case the thread function decides this thread
137 // * needs to end.
138 // *
139 // * \param sleepTimeMs The sleep time, in ms, that will be spent between
140 // * subsequent calls to the callback function. Default is 10 ms, but
141 // * other times may be set. Note that not all operating systems may be
142 // * able to schedule very short sleep times.
143 // */
144 // // int (*comp)(const void *, const void *)
145 // typedef void (*ThreadFunction) (bool& endThisThread, UINT16& sleepTimeMs);
146 //
147 // /**
148 // * The thread callback function (simpler version).
149 // *
150 // * \return True if the thread should continue to run and
151 // * continuously call this function (after potentially some waiting
152 // * time). False if this thread should end now.
153 // */
154 // // typedef boost::function < bool (void) > ThreadFunctionSimple;
155 //
156 // /// Default constructor.
157 // SickThread();
158 //
159 // /// Destructor. Will call stop() if thread is not yet stopped, and
160 // /// wait for its completion before destructing this object.
161 // ~SickThread();
162 //
163 // /// Start the thread.
164 // bool start();
165 //
166 // /// Start the thread and also set the thread function. \sa start()
167 // bool start(ThreadFunction function);
168 //
169 // /// Returns true if this thread was started and is running.
170 // bool isRunning() const;
171 //
172 // /// Stops this thread and waits for its completion.
173 // void stop();
174 //
175 // /// Set whether we want verbose debug output
176 // void setVerboseDebugOutput(bool enableVerboseDebugOutput);
177 //
178 // /// Set the thread's "run" function
179 // void setFunction(ThreadFunction threadFunction);
180 //
181 // /// Set the thread's "run" function, simpler version
182 // // void setFunctionSimple(ThreadFunctionSimple threadFunctionSimple);
183 //
184 // /// Set the sleep time between subsequent ThreadFunctionSimple calls in [milliseconds]
185 // void setSleepTimeMilliseconds(unsigned v);
186 //
187 // /// Returns the sleep time between subsequent ThreadFunctionSimple
188 // /// calls in [milliseconds]. (Default value is zero.)
189 // unsigned getSleepTimeMilliseconds() const { return m_sleepTimeMilliseconds; }
190 //
191 // private:
192 // static void* thread(void* ptr); // The thread function
193 // void thread2(); // The member thread function
194 //
195 // // typedef boost::mutex Mutex;
196 //
197 // pthread_mutex_t m_mutex; // = PTHREAD_MUTEX_INITIALIZER;
198 // // mutable Mutex m_threadMutex;
199 // // boost::condition m_threadCondition;
200 // ThreadFunction m_function;
201 // // ThreadFunctionSimple m_functionSimple;
202 //
203 // // boost::scoped_ptr<boost::thread> m_threadPtr;
204 // bool m_threadShouldRun;
205 // bool m_threadIsRunning;
206 // bool m_beVerbose;
207 // unsigned m_sleepTimeMilliseconds;
208 //
209 // // The thread
210 // pthread_t m_thread;
211 //
212 // };
213 
214 /*
215 #include <pthread.h>
216 #include <stdio.h>
217 #include <stdlib.h>
218 #include <assert.h>
219 
220 #define NUM_THREADS 5
221 
222 void *TaskCode(void *argument)
223 {
224  int tid;
225 
226  tid = *((int *) argument);
227  printf("Hello World! It's me, thread %d!\n", tid);
228 
229  // optionally: insert more useful stuff here
230 
231  return NULL;
232 }
233 
234 int main(void)
235 {
236  pthread_t threads[NUM_THREADS];
237  int thread_args[NUM_THREADS];
238  int rc, i;
239 
240  // create all threads
241  for (i=0; i<NUM_THREADS; ++i) {
242  thread_args[i] = i;
243  printf("In main: creating thread %d\n", i);
244  rc = pthread_create(&threads[i], NULL, TaskCode, (void *) &thread_args[i]);
245  assert(0 == rc);
246  }
247 
248  // wait for all threads to complete
249  for (i=0; i<NUM_THREADS; ++i) {
250  rc = pthread_join(threads[i], NULL);
251  assert(0 == rc);
252  }
253 
254  exit(EXIT_SUCCESS);
255 }
256 */
257 
258 #endif // SICKTHREAD_HPP
uint16_t UINT16
void * wrapper_prerun(void *)
Definition: SickThread.cpp:7
pthread_t * get_thread_id()
Definition: SickThread.hpp:51
uint32_t UINT32
virtual ~ThreadWrapperBase()
Definition: SickThread.hpp:24
void thread_entry()
Definition: SickThread.hpp:82
void run(void *classptr)
Definition: SickThread.hpp:26
virtual ~SickThread()
Definition: SickThread.hpp:106
virtual void thread_entry()=0
void join()
Definition: SickThread.hpp:99
friend void * wrapper_prerun(void *)
Definition: SickThread.cpp:7
Wrapper class for posix threads.
Definition: SickThread.hpp:80


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 04:55:32