Visual Servoing Platform  version 3.6.1 under development (2024-02-08)
testThread.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  * Test threading capabilities.
33  *
34 *****************************************************************************/
35 
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
47 
49 #include <iostream>
50 
51 #include <visp3/core/vpMutex.h>
52 #include <visp3/core/vpThread.h>
53 #include <visp3/core/vpTime.h>
54 
55 vpMutex g_mutex_cout;
56 
57 vpThread::Return myFooFunction(vpThread::Args args)
58 {
59  (void)(args); // Avoid warning: unused parameter args
60  // do stuff...
61  return 0;
62 }
63 
64 vpThread::Return myBarFunction(vpThread::Args args)
65 {
66  (void)(args); // Avoid warning: unused parameter args
67  // do stuff...
68  return 0;
69 }
70 
71 vpThread::Return myQuxFunction(vpThread::Args args)
72 {
73  unsigned int args_ = *((unsigned int *)args);
74  g_mutex_cout.lock();
75  std::cout << "qux arg: " << args_ << std::endl;
76  g_mutex_cout.unlock();
77  // do stuff...
78  return 0;
79 }
80 
81 int main()
82 {
83  unsigned int qux_arg = 12;
84  vpThread foo;
85  vpThread bar((vpThread::Fn)myBarFunction);
86  vpThread qux((vpThread::Fn)myQuxFunction,
87  (vpThread::Args)&qux_arg); // Pass qux_arg to myQuxFunction() function
88 
89  vpTime::wait(1000); // Sleep 1s to ensure myQuxFunction() internal printings
90  g_mutex_cout.lock();
91  std::cout << "Joinable after construction:" << std::endl;
92  std::cout << "foo: " << foo.joinable() << std::endl;
93  std::cout << "bar: " << bar.joinable() << std::endl;
94  std::cout << "qux: " << qux.joinable() << std::endl;
95  g_mutex_cout.unlock();
96 
97  foo.create((vpThread::Fn)myFooFunction);
98 
99  g_mutex_cout.lock();
100  std::cout << "Joinable after creation:" << std::endl;
101  std::cout << "foo: " << foo.joinable() << std::endl;
102  std::cout << "bar: " << bar.joinable() << std::endl;
103  std::cout << "qux: " << qux.joinable() << std::endl;
104  g_mutex_cout.unlock();
105 
106  if (foo.joinable())
107  foo.join();
108  if (bar.joinable())
109  bar.join();
110  if (qux.joinable())
111  qux.join();
112 
113  g_mutex_cout.lock();
114  std::cout << "Joinable after joining:" << std::endl;
115  std::cout << "foo: " << foo.joinable() << std::endl;
116  std::cout << "bar: " << bar.joinable() << std::endl;
117  std::cout << "qux: " << qux.joinable() << std::endl;
118  g_mutex_cout.unlock();
119 
120  return EXIT_SUCCESS;
121 }
123 
124 #else
125 
126 #include <iostream>
127 
128 int main()
129 {
130 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
131  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
132 #else
133  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
134 #endif
135  return EXIT_SUCCESS;
136 }
137 #endif
void unlock()
Definition: vpMutex.h:106
void lock()
Definition: vpMutex.h:90
void *(* Fn)(Args)
Definition: vpThread.h:74
void * Return
Definition: vpThread.h:73
bool joinable()
Definition: vpThread.h:180
void * Args
Definition: vpThread.h:72
void create(vpThread::Fn fn, vpThread::Args args=nullptr)
Definition: vpThread.h:105
void join()
Definition: vpThread.h:149
VISP_EXPORT int wait(double t0, double t)