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


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:12