threads.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Platform Check
10 *****************************************************************************/
11 
12 #include <iostream>
13 #include <ecl/config/ecl.hpp>
14 #if defined(ECL_IS_POSIX)
15 
16 /*****************************************************************************
17 ** Includes
18 *****************************************************************************/
19 
20 #include <gtest/gtest.h>
21 #include <ecl/config/ecl.hpp>
25 #include "../../include/ecl/threads/thread.hpp"
26 
27 /*****************************************************************************
28 ** Doxygen
29 *****************************************************************************/
34 /*****************************************************************************
35 ** Using
36 *****************************************************************************/
37 
39 using ecl::Thread;
41 using ecl::ref;
42 
43 /*****************************************************************************
44 ** Globals
45 *****************************************************************************/
46 
47 // Change this if you want std output.
48 static const bool output = false;
49 
50 
51 /*****************************************************************************
52 ** Namespaces
53 *****************************************************************************/
54 
55 namespace ecl {
56 namespace threads {
57 namespace tests {
58 
59 /*****************************************************************************
60 ** Classes
61 *****************************************************************************/
62 
64 public:
65  void f() {
66  if ( output ) {
67  std::cout << "A::f()" << std::endl;
68  }
69  }
70  void g(const int &i) {
71  if ( output ) {
72  std::cout << "A::g(" << i << ")" << std::endl;
73  }
74  }
75 };
76 
77 class NullaryFunction {
78 public:
79  NullaryFunction() : i(0) {};
80  typedef void result_type;
81  void operator()() {
82  for (int j = 0; j < 3; ++j ) {
83  if ( output ) {
84  std::cout << " Nullary Function Object: " << i << std::endl;
85  }
86  sleep(1);
87  ++i;
88  }
89  if ( output ) {
90  std::cout << " Nullary Function Object: finished." << std::endl;
91  }
92  }
93  int i;
94 };
95 
96 /*****************************************************************************
97 ** Functions
98 *****************************************************************************/
99 
100 void d() {
101  for (int i = 0; i < 5; ++i ) {
102  sleep(1);
103  if ( output ) {
104  std::cout << " d(): " << i << std::endl;
105  }
106  }
107  if ( output ) {
108  std::cout << " d(): finished" << std::endl;
109  }
110 }
111 void d_cb() {
112  if ( output ) {
113  std::cout << " d(): callback function" << std::endl;
114  }
115 }
116 
117 void f() {
118  for (int i = 0; i < 3; ++i) {
119  if ( output ) {
120  std::cout << "f(): " << i << std::endl;
121  }
122  }
123 }
124 
125 void g(const int &i) {
126  for (int j = 0; j < 3; ++j) {
127  if ( output ) {
128  std::cout << "g(" << i << ")" << std::endl;
129  }
130  }
131 }
132 
133 void deconstructThread() {
134  Thread thread_deconstruct(d);
135 }
136 
137 } // namespace Tests
138 } // namespace threads
139 } // namespace ecl
140 
141 /*****************************************************************************
142 ** Using
143 *****************************************************************************/
144 
145 using namespace ecl::threads::tests;
146 
147 /*****************************************************************************
148 ** Doxygen
149 *****************************************************************************/
150 
155 /*****************************************************************************
156 ** Tests
157 *****************************************************************************/
158 
159 TEST(ThreadTests,threadFunctionTypes) {
161  Thread thread_f1(f);
162  Thread thread_f2(&ThreadMemberFunctions::f,a);
163  Thread thread_f3(generateFunctionObject(f));
164  Thread thread_f4(generateFunctionObject(g,1));
165  Thread thread_f5(generateFunctionObject(&ThreadMemberFunctions::f,a));
166  Thread thread_f6(generateFunctionObject(&ThreadMemberFunctions::g,a,2));
167  thread_f1.join();
168  thread_f2.join();
169  thread_f3.join();
170  thread_f4.join();
171  thread_f5.join();
172  thread_f6.join();
173  SUCCEED();
174 }
175 
176 TEST(ThreadTests,cancelThread) {
177  Thread thread(generateFunctionObject(d));
178  sleep(3);
179  EXPECT_TRUE(thread.isRunning());
180 // std::cout << "Cancelling d() thread (you should see no further output)." << std::endl;
181  thread.cancel();
182  EXPECT_FALSE(thread.isRunning());
183  thread.join();
184  EXPECT_FALSE(thread.isRunning());
185 }
186 
187 TEST(ThreadTests,deconstruct) {
189  sleep(6);
190  SUCCEED();
191 }
192 
193 TEST(ThreadTests,nullaryFunctionObjects) {
194  NullaryFunction function_object;
195  Thread thread_f8(function_object); // Thread thread_f8 = NullaryFunction();
196  sleep(4);
197  Thread thread_f9(ref(function_object));
198  sleep(4);
199  thread_f8.join();
200  thread_f9.join();
201  SUCCEED();
202 }
203 
204 TEST(ThreadTests,stackSize) {
205  Thread thread(f,ecl::DefaultPriority,1024*1024);
206  thread.join();
207  SUCCEED();
208 }
209 
210 TEST(ThreadTests,delayedStart) {
211 
212  NullaryFunction function_object;
214  Thread thread1, thread2, thread3, thread4;
215  thread1.start(f);
216  thread2.start(function_object);
217  thread3.start(&ThreadMemberFunctions::f,a);
219  thread1.join();
220  thread2.join();
221  thread3.join();
222  thread4.join();
223  SUCCEED();
224 }
225 
226 /*****************************************************************************
227 ** Main program
228 *****************************************************************************/
229 
230 int main(int argc, char **argv) {
231 
232  testing::InitGoogleTest(&argc,argv);
233  return RUN_ALL_TESTS();
234 }
235 
236 #else
237 
238 /*****************************************************************************
239 ** Alternative main
240 *****************************************************************************/
241 
242 int main(int argc, char **argv) {
243 
244  std::cout << "Currently not supported on your platform." << std::endl;
245 }
246 
247 #endif /* ECL_IS_POSIX */
248 
d_cb
void d_cb()
Definition: thread.cpp:56
TEST
TEST(TypeTests, fundamentals)
function_objects.hpp
g
void g(const int &i)
Definition: thread.cpp:66
NullaryFunction
Definition: thread.cpp:32
f
void f()
Definition: examples/mutex.cpp:27
ThreadMemberFunctions::g
void g(const int &i)
Definition: thread.cpp:31
ecl::DefaultPriority
@ DefaultPriority
Definition: priority_common.hpp:43
d
void d()
Definition: thread.cpp:48
ecl::StandardException
ThreadMemberFunctions
Definition: thread.cpp:22
references.hpp
standard_exception.hpp
deconstructThread
void deconstructThread()
Definition: thread.cpp:72
ThreadMemberFunctions::f
void f()
Definition: thread.cpp:28
ecl::ref
ReferenceWrapper< T > ref(T &wrapped_object)
main
int main(int argc, char **argv)
Definition: threads.cpp:242
ecl
Embedded control libraries.
ecl::generateFunctionObject
NullaryFreeFunction< R > generateFunctionObject(R(*function)())


ecl_threads
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:43