Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testThread.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
47 #include <visp3/core/vpConfig.h>
48 
49 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
50 
52 #include <iostream>
53 
54 #include <visp3/core/vpThread.h>
55 #include <visp3/core/vpTime.h>
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  std::cout << "qux arg: " << args_ << std::endl;
75  // do stuff...
76  return 0;
77 }
78 
79 int main()
80 {
81  unsigned int qux_arg = 12;
82  vpThread foo;
83  vpThread bar((vpThread::Fn)myBarFunction);
84  vpThread qux((vpThread::Fn)myQuxFunction,
85  (vpThread::Args)&qux_arg); // Pass qux_arg to myQuxFunction() function
86 
87  vpTime::wait(1000); // Sleep 1s to ensure myQuxFunction() internal printings
88  std::cout << "Joinable after construction:" << std::endl;
89  std::cout << "foo: " << foo.joinable() << std::endl;
90  std::cout << "bar: " << bar.joinable() << std::endl;
91  std::cout << "qux: " << qux.joinable() << std::endl;
92 
93  foo.create((vpThread::Fn)myFooFunction);
94 
95  std::cout << "Joinable after creation:" << std::endl;
96  std::cout << "foo: " << foo.joinable() << std::endl;
97  std::cout << "bar: " << bar.joinable() << std::endl;
98  std::cout << "qux: " << qux.joinable() << std::endl;
99 
100  if (foo.joinable())
101  foo.join();
102  if (bar.joinable())
103  bar.join();
104  if (qux.joinable())
105  qux.join();
106 
107  std::cout << "Joinable after joining:" << std::endl;
108  std::cout << "foo: " << foo.joinable() << std::endl;
109  std::cout << "bar: " << bar.joinable() << std::endl;
110  std::cout << "qux: " << qux.joinable() << std::endl;
111 
112  return 0;
113 }
115 
116 #else
117 
118 #include <iostream>
119 
120 int main()
121 {
122 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
123  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
124 #else
125  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
126 #endif
127 }
128 #endif
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void * Return
Definition: vpThread.h:78
void *(* Fn)(Args)
Definition: vpThread.h:79
void create(vpThread::Fn fn, vpThread::Args args=NULL)
Definition: vpThread.h:110
void * Args
Definition: vpThread.h:77
void join()
Definition: vpThread.h:154
bool joinable()
Definition: vpThread.h:185