Visual Servoing Platform  version 3.1.0
vpThread.h
1 #ifndef __vpPthread_h_
2 #define __vpPthread_h_
3 
4 #include <visp3/core/vpConfig.h>
5 #include <visp3/core/vpException.h>
6 
7 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
8 
9 #if defined(VISP_HAVE_PTHREAD)
10 #include <pthread.h>
11 #include <string.h>
12 #elif defined(_WIN32)
13 // Include WinSock2.h before windows.h to ensure that winsock.h is not
14 // included by windows.h since winsock.h and winsock2.h are incompatible
15 #include <WinSock2.h>
16 #include <windows.h>
17 #endif
18 
36 class vpThread
37 {
38 public:
39 #if defined(VISP_HAVE_PTHREAD)
40  typedef void *Args;
41  typedef void *Return;
42  typedef void *(*Fn)(Args);
43  typedef pthread_t Handle;
44 #elif defined(_WIN32)
45  typedef LPVOID Args;
46  typedef DWORD Return;
47  typedef LPTHREAD_START_ROUTINE Fn;
48  // typedef DWORD (*Fn)(Args);
49  typedef HANDLE Handle;
50 #endif
51 
55  vpThread() : m_handle(), m_isCreated(false), m_isJoinable(false) {}
56 
64  {
65  create(fn, args);
66  }
67 
73  void create(vpThread::Fn fn, vpThread::Args args = NULL)
74  {
75  if (m_isCreated)
76  throw vpException(vpException::fatalError, "The thread is already created");
77 #if defined(VISP_HAVE_PTHREAD)
78  int err = pthread_create(&m_handle, NULL, fn, args);
79  if (err != 0) {
80  throw vpException(vpException::cannotUseConstructorError, "Can't create thread : %s", strerror(err));
81  }
82 #elif defined(_WIN32)
83  DWORD dwThreadIdArray;
84  m_handle = CreateThread(NULL, // default security attributes
85  0, // use default stack size
86  fn, // thread function name
87  args, // argument to thread function
88  0, // use default creation flags
89  &dwThreadIdArray); // returns the thread identifier
90 #endif
91 
92  m_isJoinable = true;
93  }
94 
98  virtual ~vpThread()
99  {
100  join();
101 #if defined(VISP_HAVE_PTHREAD)
102 #elif defined(_WIN32)
103  CloseHandle(m_handle);
104 #endif
105  }
106 
117  void join()
118  {
119  if (m_isJoinable) {
120 #if defined(VISP_HAVE_PTHREAD)
121  pthread_join(m_handle, NULL);
122 #elif defined(_WIN32)
123 #if defined(WINRT_8_1)
124  WaitForSingleObjectEx(m_handle, INFINITE, FALSE);
125 #else
126  WaitForSingleObject(m_handle, INFINITE);
127 #endif
128 #endif
129  m_isJoinable = false;
130  }
131  }
132 
137  Handle getHandle() { return m_handle; }
138 
148  bool joinable() { return m_isJoinable; }
149 
150 protected:
151  Handle m_handle;
152  bool m_isCreated;
154 };
155 
156 #endif
157 #endif
pthread_t Handle
Definition: vpThread.h:43
virtual ~vpThread()
Definition: vpThread.h:98
void * Return
Definition: vpThread.h:41
error that can be emited by ViSP classes.
Definition: vpException.h:71
Handle m_handle
Thread handle.
Definition: vpThread.h:151
void *(* Fn)(Args)
Definition: vpThread.h:42
void create(vpThread::Fn fn, vpThread::Args args=NULL)
Definition: vpThread.h:73
void * Args
Definition: vpThread.h:40
bool m_isCreated
Indicates if the thread is created.
Definition: vpThread.h:152
void join()
Definition: vpThread.h:117
Handle getHandle()
Definition: vpThread.h:137
vpThread()
Definition: vpThread.h:55
bool m_isJoinable
Indicates if the thread is joinable.
Definition: vpThread.h:153
bool joinable()
Definition: vpThread.h:148
vpThread(vpThread::Fn fn, vpThread::Args args=NULL)
Definition: vpThread.h:63