src
examples
thread.cpp
Go to the documentation of this file.
1
8
/*****************************************************************************
9
** Platform Check
10
*****************************************************************************/
11
12
#include <
ecl/config.hpp
>
13
14
/*****************************************************************************
15
** Includes
16
*****************************************************************************/
17
18
#include <iostream>
19
#include "../../include/ecl/threads/thread.hpp"
20
#include <
ecl/time/sleep.hpp
>
21
22
class
ThreadMemberFunctions
{
23
public
:
24
void
f
() {
25
std::cout <<
"A::f()"
<< std::endl;
26
}
27
void
g
(
const
int
&i) {
28
std::cout <<
"A::g("
<< i <<
")"
<< std::endl;
29
}
30
};
31
32
class
NullaryFunction
{
33
public
:
34
NullaryFunction
() :
i
(0) {};
35
typedef
void
result_type
;
36
void
operator()
() {
37
for
(
int
j = 0; j < 3; ++j ) {
38
std::cout <<
" Nullary Function Object: "
<<
i
<< std::endl;
39
ecl::Sleep sleep;
40
sleep(1);
41
++
i
;
42
}
43
std::cout <<
" Nullary Function Object: finished."
<< std::endl;
44
}
45
int
i
;
46
};
47
48
void
d
() {
49
for
(
int
i = 0; i < 5; ++i ) {
50
ecl::Sleep sleep;
51
sleep(1);
52
std::cout <<
" d(): "
<< i << std::endl;
53
}
54
std::cout <<
" d(): finished"
<< std::endl;
55
}
56
void
d_cb
() {
57
std::cout <<
" d(): callback function"
<< std::endl;
58
}
59
60
void
f
() {
61
for
(
int
i = 0; i < 3; ++i) {
62
std::cout <<
"f(): "
<< i << std::endl;
63
}
64
}
65
66
void
g
(
const
int
&i) {
67
for
(
int
j = 0; j < 3; ++j) {
68
std::cout <<
"g("
<< i <<
")"
<< std::endl;
69
}
70
}
71
72
void
deconstructThread
() {
73
ecl::Thread thread_deconstruct(
d
);
74
}
75
76
int
main
() {
77
78
std::cout << std::endl;
79
std::cout <<
"***********************************************************"
<< std::endl;
80
std::cout <<
" Thread Function Type"
<< std::endl;
81
std::cout <<
"***********************************************************"
<< std::endl;
82
std::cout << std::endl;
83
84
ThreadMemberFunctions
a;
85
ecl::Thread thread_f1(
f
);
86
ecl::Thread thread_f2(&
ThreadMemberFunctions::f
, a);
87
ecl::Thread thread_f3(
ecl::generateFunctionObject
(
f
));
88
ecl::Thread thread_f4(
ecl::generateFunctionObject
(
g
,1));
89
ecl::Thread thread_f5(
ecl::generateFunctionObject
(&
ThreadMemberFunctions::f
,a));
90
ecl::Thread thread_f6(
ecl::generateFunctionObject
(&
ThreadMemberFunctions::g
,a,2));
91
92
thread_f1.join();
93
thread_f2.join();
94
thread_f3.join();
95
thread_f4.join();
96
thread_f5.join();
97
thread_f6.join();
98
99
std::cout << std::endl;
100
std::cout <<
"***********************************************************"
<< std::endl;
101
std::cout <<
" Cancel"
<< std::endl;
102
std::cout <<
"***********************************************************"
<< std::endl;
103
std::cout << std::endl;
104
105
ecl::Thread thread(
ecl::generateFunctionObject
(
d
));
106
ecl::Sleep sleep;
107
sleep(3);
108
if
(!thread.isRunning()) {
109
std::cout <<
"Abnormal #1"
<< std::endl;
110
}
111
thread.cancel();
112
if
(thread.isRunning()) {
113
std::cout <<
"Abnormal #2"
<< std::endl;
114
}
115
thread.join();
116
if
(thread.isRunning()) {
117
std::cout <<
"Abnormal #3"
<< std::endl;
118
}
119
120
std::cout << std::endl;
121
std::cout <<
"***********************************************************"
<< std::endl;
122
std::cout <<
" Deconstruct"
<< std::endl;
123
std::cout <<
"***********************************************************"
<< std::endl;
124
std::cout << std::endl;
125
126
deconstructThread
();
127
sleep(6);
128
129
std::cout << std::endl;
130
std::cout <<
"***********************************************************"
<< std::endl;
131
std::cout <<
" Nullary Function Objects"
<< std::endl;
132
std::cout <<
"***********************************************************"
<< std::endl;
133
std::cout << std::endl;
134
135
NullaryFunction
function_object_1;
136
ecl::Thread thread_f8(function_object_1);
137
sleep(4);
138
ecl::Thread thread_f9(
ecl::ref
(function_object_1));
139
sleep(4);
140
thread_f8.join();
141
thread_f9.join();
142
143
std::cout << std::endl;
144
std::cout <<
"***********************************************************"
<< std::endl;
145
std::cout <<
" Delayed Start"
<< std::endl;
146
std::cout <<
"***********************************************************"
<< std::endl;
147
std::cout << std::endl;
148
149
NullaryFunction
function_object_2;
150
ThreadMemberFunctions
b;
151
ecl::Thread thread1, thread2, thread3, thread4;
152
thread1.start(
f
);
153
thread2.start(function_object_2);
154
thread3.start(&
ThreadMemberFunctions::f
,b);
155
thread4.start(
ecl::generateFunctionObject
(&
ThreadMemberFunctions::g
,b,2));
156
157
thread1.join();
158
thread2.join();
159
thread3.join();
160
thread4.join();
161
162
std::cout << std::endl;
163
std::cout <<
"***********************************************************"
<< std::endl;
164
std::cout <<
" Passed"
<< std::endl;
165
std::cout <<
"***********************************************************"
<< std::endl;
166
std::cout << std::endl;
167
168
return
0;
169
}
d_cb
void d_cb()
Definition:
thread.cpp:56
f
void f()
Definition:
thread.cpp:60
sleep.hpp
g
void g(const int &i)
Definition:
thread.cpp:66
NullaryFunction
Definition:
thread.cpp:32
NullaryFunction::i
int i
Definition:
thread.cpp:45
ThreadMemberFunctions::g
void g(const int &i)
Definition:
thread.cpp:31
NullaryFunction::result_type
void result_type
Definition:
thread.cpp:34
d
void d()
Definition:
thread.cpp:48
ThreadMemberFunctions
Definition:
thread.cpp:22
config.hpp
deconstructThread
void deconstructThread()
Definition:
thread.cpp:72
ThreadMemberFunctions::f
void f()
Definition:
thread.cpp:28
ecl::ref
ReferenceWrapper< T > ref(T &wrapped_object)
NullaryFunction::operator()
void operator()()
Definition:
thread.cpp:36
main
int main()
Definition:
thread.cpp:76
ecl::generateFunctionObject
NullaryFreeFunction< R > generateFunctionObject(R(*function)())
NullaryFunction::NullaryFunction
NullaryFunction()
Definition:
thread.cpp:34
ecl_threads
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:43