YDLIDAR SDK  V1.4.5
thread.h
1 #pragma once
2 #include "v8stdint.h"
3 
4 #ifdef _WIN32
5 #include <conio.h>
6 #include <windows.h>
7 #include <io.h>
8 #include <process.h>
9 #else
10 #include <pthread.h>
11 #include <assert.h>
12 #endif
13 
14 #define UNUSED(x) (void)x
15 
16 #if defined(__ANDROID__)
17 #define pthread_cancel(x) 0
18 #endif
19 
20 #define CLASS_THREAD(c , x ) Thread::ThreadCreateObjectFunctor<c, &c::x>(this)
21 
22 class Thread {
23  public:
24 
25  template <class CLASS, int (CLASS::*PROC)(void)> static Thread
26  ThreadCreateObjectFunctor(CLASS *pthis) {
27  return createThread(createThreadAux<CLASS, PROC>, pthis);
28  }
29 
30  template <class CLASS, int (CLASS::*PROC)(void) > static _size_t THREAD_PROC
31  createThreadAux(void *param) {
32  return (static_cast<CLASS *>(param)->*PROC)();
33  }
34 
35  static Thread createThread(thread_proc_t proc, void *param = NULL) {
36  Thread thread_(proc, param);
37 #if defined(_WIN32)
38  thread_._handle = (_size_t)(_beginthreadex(NULL, 0,
39  (unsigned int (__stdcall *)(void *))proc, param, 0, NULL));
40 #else
41  assert(sizeof(thread_._handle) >= sizeof(pthread_t));
42 
43  pthread_create((pthread_t *)&thread_._handle, NULL, (void *(*)(void *))proc,
44  param);
45 #endif
46  return thread_;
47  }
48 
49  public:
50  explicit Thread(): _param(NULL), _func(NULL), _handle(0) {}
51  virtual ~Thread() {}
52  _size_t getHandle() {
53  return _handle;
54  }
55  int terminate() {
56 #if defined(_WIN32)
57 
58  if (!this->_handle) {
59  return 0;
60  }
61 
62  if (TerminateThread(reinterpret_cast<HANDLE>(this->_handle), -1)) {
63  CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
64  this->_handle = NULL;
65  return 0;
66  } else {
67  return -2;
68  }
69 
70 #else
71 
72  if (!this->_handle) {
73  return 0;
74  }
75 
76  return pthread_cancel((pthread_t)this->_handle);
77 #endif
78  }
79  void *getParam() {
80  return _param;
81  }
82  int join(unsigned long timeout = -1) {
83  if (!this->_handle) {
84  return 0;
85  }
86 
87 #if defined(_WIN32)
88 
89  switch (WaitForSingleObject(reinterpret_cast<HANDLE>(this->_handle), timeout)) {
90  case WAIT_OBJECT_0:
91  CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
92  this->_handle = NULL;
93  return 0;
94 
95  case WAIT_ABANDONED:
96  return -2;
97 
98  case WAIT_TIMEOUT:
99  return -1;
100  }
101 
102 #else
103  UNUSED(timeout);
104  void *res;
105  int s = -1;
106  s = pthread_cancel((pthread_t)(this->_handle));
107 
108  if (s != 0) {
109  }
110 
111  s = pthread_join((pthread_t)(this->_handle), &res);
112 
113  if (s != 0) {
114  }
115 
116  if (res == PTHREAD_CANCELED) {
117  printf("%lu thread has been canceled\n", this->_handle);
118  this->_handle = 0;
119  }
120 
121 #endif
122  return 0;
123  }
124 
125  bool operator== (const Thread &right) {
126  return this->_handle == right._handle;
127  }
128  protected:
129  explicit Thread(thread_proc_t proc, void *param): _param(param), _func(proc),
130  _handle(0) {}
131  void *_param;
132  thread_proc_t _func;
133  _size_t _handle;
134 };
135 
Definition: thread.h:22