Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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_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
69{
70public:
71#if defined(VISP_HAVE_PTHREAD)
72 typedef void *Args;
73 typedef void *Return;
74 typedef void *(*Fn)(Args);
75 typedef pthread_t Handle;
76#elif defined(_WIN32)
77 typedef LPVOID Args;
78 typedef DWORD Return;
79 typedef LPTHREAD_START_ROUTINE Fn;
80 // typedef DWORD (*Fn)(Args);
81 typedef HANDLE Handle;
82#endif
87 vpThread() : m_handle(), m_isCreated(false), m_isJoinable(false) { }
88
96 {
97 create(fn, args);
98 }
99
105 void create(vpThread::Fn fn, vpThread::Args args = NULL)
106 {
107 if (m_isCreated)
108 throw vpException(vpException::fatalError, "The thread is already created");
109#if defined(VISP_HAVE_PTHREAD)
110 int err = pthread_create(&m_handle, NULL, fn, args);
111 if (err != 0) {
112 throw vpException(vpException::cannotUseConstructorError, "Can't create thread : %s", strerror(err));
113 }
114#elif defined(_WIN32)
115 DWORD dwThreadIdArray;
116 m_handle = CreateThread(NULL, // default security attributes
117 0, // use default stack size
118 fn, // thread function name
119 args, // argument to thread function
120 0, // use default creation flags
121 &dwThreadIdArray); // returns the thread identifier
122#endif
123
124 m_isJoinable = true;
125 }
126
130 virtual ~vpThread()
131 {
132 join();
133#if defined(VISP_HAVE_PTHREAD)
134#elif defined(_WIN32)
135 CloseHandle(m_handle);
136#endif
137 }
138
149 void join()
150 {
151 if (m_isJoinable) {
152#if defined(VISP_HAVE_PTHREAD)
153 pthread_join(m_handle, NULL);
154#elif defined(_WIN32)
155#if defined(WINRT_8_1)
156 WaitForSingleObjectEx(m_handle, INFINITE, FALSE);
157#else
158 WaitForSingleObject(m_handle, INFINITE);
159#endif
160#endif
161 m_isJoinable = false;
162 }
163 }
164
170
180 bool joinable() { return m_isJoinable; }
181
182protected:
186};
187
188#endif
189#endif
error that can be emitted by ViSP classes.
Definition vpException.h:59
@ cannotUseConstructorError
Contructor error.
Definition vpException.h:80
@ fatalError
Fatal error.
Definition vpException.h:84
bool m_isCreated
Indicates if the thread is created.
Definition vpThread.h:184
virtual ~vpThread()
Definition vpThread.h:130
bool joinable()
Definition vpThread.h:180
void *(*) Fn(Args)
Definition vpThread.h:74
Handle getHandle()
Definition vpThread.h:169
pthread_t Handle
Definition vpThread.h:75
void * Args
Definition vpThread.h:72
void * Return
Definition vpThread.h:73
void create(vpThread::Fn fn, vpThread::Args args=NULL)
Definition vpThread.h:105
vpThread(vpThread::Fn fn, vpThread::Args args=NULL)
Definition vpThread.h:95
vpThread()
Definition vpThread.h:87
Handle m_handle
Thread handle.
Definition vpThread.h:183
bool m_isJoinable
Indicates if the thread is joinable.
Definition vpThread.h:185
void join()
Definition vpThread.h:149