Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpThread.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Threading capabilities
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 #ifndef _vpPthread_h_
39 #define _vpPthread_h_
40 
41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpException.h>
43 
44 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
45 
46 #if defined(VISP_HAVE_PTHREAD)
47 #include <pthread.h>
48 #include <string.h>
49 #elif defined(_WIN32)
50 // Include WinSock2.h before windows.h to ensure that winsock.h is not
51 // included by windows.h since winsock.h and winsock2.h are incompatible
52 #include <WinSock2.h>
53 #include <windows.h>
54 #endif
55 
73 class vpThread
74 {
75 public:
76 #if defined(VISP_HAVE_PTHREAD)
77  typedef void *Args;
78  typedef void *Return;
79  typedef void *(*Fn)(Args);
80  typedef pthread_t Handle;
81 #elif defined(_WIN32)
82  typedef LPVOID Args;
83  typedef DWORD Return;
84  typedef LPTHREAD_START_ROUTINE Fn;
85  // typedef DWORD (*Fn)(Args);
86  typedef HANDLE Handle;
87 #endif
88 
92  vpThread() : m_handle(), m_isCreated(false), m_isJoinable(false) {}
93 
101  {
102  create(fn, args);
103  }
104 
110  void create(vpThread::Fn fn, vpThread::Args args = NULL)
111  {
112  if (m_isCreated)
113  throw vpException(vpException::fatalError, "The thread is already created");
114 #if defined(VISP_HAVE_PTHREAD)
115  int err = pthread_create(&m_handle, NULL, fn, args);
116  if (err != 0) {
117  throw vpException(vpException::cannotUseConstructorError, "Can't create thread : %s", strerror(err));
118  }
119 #elif defined(_WIN32)
120  DWORD dwThreadIdArray;
121  m_handle = CreateThread(NULL, // default security attributes
122  0, // use default stack size
123  fn, // thread function name
124  args, // argument to thread function
125  0, // use default creation flags
126  &dwThreadIdArray); // returns the thread identifier
127 #endif
128 
129  m_isJoinable = true;
130  }
131 
135  virtual ~vpThread()
136  {
137  join();
138 #if defined(VISP_HAVE_PTHREAD)
139 #elif defined(_WIN32)
140  CloseHandle(m_handle);
141 #endif
142  }
143 
154  void join()
155  {
156  if (m_isJoinable) {
157 #if defined(VISP_HAVE_PTHREAD)
158  pthread_join(m_handle, NULL);
159 #elif defined(_WIN32)
160 #if defined(WINRT_8_1)
161  WaitForSingleObjectEx(m_handle, INFINITE, FALSE);
162 #else
163  WaitForSingleObject(m_handle, INFINITE);
164 #endif
165 #endif
166  m_isJoinable = false;
167  }
168  }
169 
174  Handle getHandle() { return m_handle; }
175 
185  bool joinable() { return m_isJoinable; }
186 
187 protected:
188  Handle m_handle;
189  bool m_isCreated;
191 };
192 
193 #endif
194 #endif
pthread_t Handle
Definition: vpThread.h:80
virtual ~vpThread()
Definition: vpThread.h:135
void * Return
Definition: vpThread.h:78
error that can be emited by ViSP classes.
Definition: vpException.h:71
Handle m_handle
Thread handle.
Definition: vpThread.h:188
void *(* Fn)(Args)
Definition: vpThread.h:79
void create(vpThread::Fn fn, vpThread::Args args=NULL)
Definition: vpThread.h:110
void * Args
Definition: vpThread.h:77
bool m_isCreated
Indicates if the thread is created.
Definition: vpThread.h:189
void join()
Definition: vpThread.h:154
Handle getHandle()
Definition: vpThread.h:174
vpThread()
Definition: vpThread.h:92
bool m_isJoinable
Indicates if the thread is joinable.
Definition: vpThread.h:190
bool joinable()
Definition: vpThread.h:185
vpThread(vpThread::Fn fn, vpThread::Args args=NULL)
Definition: vpThread.h:100