Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <windows.h>
14 #endif
15 
31 class vpThread
32 {
33 public:
34 #if defined(VISP_HAVE_PTHREAD)
35  typedef void *Args;
36  typedef void *Return;
37  typedef void *(*Fn)(Args);
38  typedef pthread_t Handle;
39 #elif defined(_WIN32)
40  typedef LPVOID Args;
41  typedef DWORD Return;
42  typedef LPTHREAD_START_ROUTINE Fn;
43  //typedef DWORD (*Fn)(Args);
44  typedef HANDLE Handle;
45 #endif
46 
50  vpThread() : m_handle(), m_isCreated(false), m_isJoinable(false)
51  {
52  }
53 
61  {
62  create(fn, args);
63  }
64 
70  void create(vpThread::Fn fn, vpThread::Args args=NULL)
71  {
72  if (m_isCreated)
73  throw vpException(vpException::fatalError, "The thread is already created");
74 #if defined(VISP_HAVE_PTHREAD)
75  int err = pthread_create(&m_handle, NULL, fn, args);
76  if (err != 0) {
78  "Can't create thread : %s", strerror(err));
79  }
80 #elif defined(_WIN32)
81  DWORD dwThreadIdArray;
82  m_handle = CreateThread(
83  NULL, // default security attributes
84  0, // use default stack size
85  fn, // thread function name
86  args, // argument to thread function
87  0, // use default creation flags
88  &dwThreadIdArray); // returns the thread identifier
89 #endif
90 
91  m_isJoinable = true;
92  }
93 
97  virtual ~vpThread()
98  {
99  join();
100 #if defined(VISP_HAVE_PTHREAD)
101 #elif defined(_WIN32)
102  CloseHandle(m_handle);
103 #endif
104  }
105 
106 
116  void join()
117  {
118  if (m_isJoinable) {
119 #if defined(VISP_HAVE_PTHREAD)
120  pthread_join(m_handle, NULL);
121 #elif defined(_WIN32)
122 # if defined(WINRT_8_1)
123  WaitForSingleObjectEx(m_handle, INFINITE, FALSE);
124 # else
125  WaitForSingleObject(m_handle, INFINITE);
126 # endif
127 #endif
128  m_isJoinable = false;
129  }
130  }
131 
136  {
137  return m_handle;
138  }
139 
149  bool joinable()
150  {
151  return m_isJoinable;
152  }
153 
154 protected:
156  bool m_isCreated;
158 };
159 
160 #endif
161 #endif
pthread_t Handle
Definition: vpThread.h:38
virtual ~vpThread()
Definition: vpThread.h:97
void * Return
Definition: vpThread.h:36
error that can be emited by ViSP classes.
Definition: vpException.h:73
Handle m_handle
Thread handle.
Definition: vpThread.h:155
void *(* Fn)(Args)
Definition: vpThread.h:37
void create(vpThread::Fn fn, vpThread::Args args=NULL)
Definition: vpThread.h:70
void * Args
Definition: vpThread.h:35
bool m_isCreated
Indicates if the thread is created.
Definition: vpThread.h:156
void join()
Definition: vpThread.h:116
Handle getHandle()
Definition: vpThread.h:135
vpThread()
Definition: vpThread.h:50
bool m_isJoinable
Indicates if the thread is joinable.
Definition: vpThread.h:157
bool joinable()
Definition: vpThread.h:149
vpThread(vpThread::Fn fn, vpThread::Args args=NULL)
Definition: vpThread.h:60