Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
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 
63 class vp_deprecated vpThread
64 {
65 public:
66 #if defined(VISP_HAVE_PTHREAD)
67  typedef void *Args;
68  typedef void *Return;
69  typedef void *(*Fn)(Args);
70  typedef pthread_t Handle;
71 #elif defined(_WIN32)
72  typedef LPVOID Args;
73  typedef DWORD Return;
74  typedef LPTHREAD_START_ROUTINE Fn;
75  // typedef DWORD (*Fn)(Args);
76  typedef HANDLE Handle;
77 #endif
82  vpThread() : m_handle(), m_isCreated(false), m_isJoinable(false) { }
83 
90  vpThread(vpThread::Fn fn, vpThread::Args args = nullptr) : m_handle(), m_isCreated(false), m_isJoinable(false)
91  {
92  create(fn, args);
93  }
94 
100  void create(vpThread::Fn fn, vpThread::Args args = nullptr)
101  {
102  if (m_isCreated)
103  throw vpException(vpException::fatalError, "The thread is already created");
104 #if defined(VISP_HAVE_PTHREAD)
105  int err = pthread_create(&m_handle, nullptr, fn, args);
106  if (err != 0) {
107  throw vpException(vpException::cannotUseConstructorError, "Can't create thread : %s", strerror(err));
108  }
109 #elif defined(_WIN32)
110  DWORD dwThreadIdArray;
111  m_handle = CreateThread(nullptr, // default security attributes
112  0, // use default stack size
113  fn, // thread function name
114  args, // argument to thread function
115  0, // use default creation flags
116  &dwThreadIdArray); // returns the thread identifier
117 #endif
118 
119  m_isJoinable = true;
120  }
121 
125  virtual ~vpThread()
126  {
127  join();
128 #if defined(VISP_HAVE_PTHREAD)
129 #elif defined(_WIN32)
130  CloseHandle(m_handle);
131 #endif
132  }
133 
144  void join()
145  {
146  if (m_isJoinable) {
147 #if defined(VISP_HAVE_PTHREAD)
148  pthread_join(m_handle, nullptr);
149 #elif defined(_WIN32)
150 #if defined(WINRT_8_1)
151  WaitForSingleObjectEx(m_handle, INFINITE, FALSE);
152 #else
153  WaitForSingleObject(m_handle, INFINITE);
154 #endif
155 #endif
156  m_isJoinable = false;
157  }
158  }
159 
164  Handle getHandle() { return m_handle; }
165 
175  bool joinable() { return m_isJoinable; }
176 
177 protected:
179  bool m_isCreated;
181 };
182 
183 #endif
184 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ cannotUseConstructorError
constructor error
Definition: vpException.h:80
@ fatalError
Fatal error.
Definition: vpException.h:84
vpThread(vpThread::Fn fn, vpThread::Args args=nullptr)
Definition: vpThread.h:90
void *(* Fn)(Args)
Definition: vpThread.h:69
void * Return
Definition: vpThread.h:68
bool m_isCreated
Indicates if the thread is created.
Definition: vpThread.h:179
virtual ~vpThread()
Definition: vpThread.h:125
bool joinable()
Definition: vpThread.h:175
Handle getHandle()
Definition: vpThread.h:164
void * Args
Definition: vpThread.h:67
pthread_t Handle
Definition: vpThread.h:70
vpThread()
Definition: vpThread.h:82
Handle m_handle
Thread handle.
Definition: vpThread.h:178
void create(vpThread::Fn fn, vpThread::Args args=nullptr)
Definition: vpThread.h:100
bool m_isJoinable
Indicates if the thread is joinable.
Definition: vpThread.h:180
void join()
Definition: vpThread.h:144