Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpThread.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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// Mute warning with clang-cl
46// warning : non-portable path to file '<WinSock2.h>'; specified path differs in case from file name on disk [-Wnonportable-system-include-path]
47// warning : non-portable path to file '<Windows.h>'; specified path differs in case from file name on disk [-Wnonportable-system-include-path]
48#if defined(__clang__)
49# pragma clang diagnostic push
50# pragma clang diagnostic ignored "-Wnonportable-system-include-path"
51#endif
52
53// Include WinSock2.h before windows.h to ensure that winsock.h is not
54// included by windows.h since winsock.h and winsock2.h are incompatible
55#include <WinSock2.h>
56#include <windows.h>
57
58#if defined(__clang__)
59# pragma clang diagnostic pop
60#endif
61
62#endif
63
64#ifdef ENABLE_VISP_NAMESPACE
65namespace VISP_NAMESPACE_NAME
66{
67#endif
80class VP_DEPRECATED vpThread
81{
82public:
83#if defined(VISP_HAVE_PTHREAD)
84 typedef void *Args;
85 typedef void *Return;
86 typedef void *(*Fn)(Args);
87 typedef pthread_t Handle;
88#elif defined(_WIN32)
89 typedef LPVOID Args;
90 typedef DWORD Return;
91 typedef LPTHREAD_START_ROUTINE Fn;
92 // typedef DWORD (*Fn)(Args);
93 typedef HANDLE Handle;
94#endif
99 vpThread() : m_handle(), m_isCreated(false), m_isJoinable(false) { }
100
108 vpThread(vpThread::Fn fn, vpThread::Args args = nullptr) : m_handle(), m_isCreated(false), m_isJoinable(false)
109 {
110 create(fn, args);
111 }
112
119 void create(vpThread::Fn fn, vpThread::Args args = nullptr)
120 {
121 if (m_isCreated)
122 throw vpException(vpException::fatalError, "The thread is already created");
123#if defined(VISP_HAVE_PTHREAD)
124 int err = pthread_create(&m_handle, nullptr, fn, args);
125 if (err != 0) {
126 throw vpException(vpException::cannotUseConstructorError, "Can't create thread : %s", strerror(err));
127 }
128#elif defined(_WIN32)
129 DWORD dwThreadIdArray;
130 m_handle = CreateThread(nullptr, // default security attributes
131 0, // use default stack size
132 fn, // thread function name
133 args, // argument to thread function
134 0, // use default creation flags
135 &dwThreadIdArray); // returns the thread identifier
136#endif
137
138 m_isJoinable = true;
139 }
140
144 virtual ~vpThread()
145 {
146 join();
147#if defined(VISP_HAVE_PTHREAD)
148#elif defined(_WIN32)
149 CloseHandle(m_handle);
150#endif
151 }
152
163 void join()
164 {
165 if (m_isJoinable) {
166#if defined(VISP_HAVE_PTHREAD)
167 pthread_join(m_handle, nullptr);
168#elif defined(_WIN32)
169#if defined(WINRT_8_1)
170 WaitForSingleObjectEx(m_handle, INFINITE, FALSE);
171#else
172 WaitForSingleObject(m_handle, INFINITE);
173#endif
174#endif
175 m_isJoinable = false;
176 }
177 }
178
184
194 bool joinable() { return m_isJoinable; }
195
196protected:
200};
201#ifdef ENABLE_VISP_NAMESPACE
202}
203#endif
204#endif
205#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:108
void * Return
Definition vpThread.h:85
bool m_isCreated
Indicates if the thread is created.
Definition vpThread.h:198
virtual ~vpThread()
Definition vpThread.h:144
void *(* Fn)(Args)
Definition vpThread.h:86
bool joinable()
Definition vpThread.h:194
Handle getHandle()
Definition vpThread.h:183
void * Args
Definition vpThread.h:84
pthread_t Handle
Definition vpThread.h:87
vpThread()
Definition vpThread.h:99
Handle m_handle
Thread handle.
Definition vpThread.h:197
void create(vpThread::Fn fn, vpThread::Args args=nullptr)
Definition vpThread.h:119
bool m_isJoinable
Indicates if the thread is joinable.
Definition vpThread.h:199
void join()
Definition vpThread.h:163