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