-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCtorByMethodPtr.cpp
More file actions
92 lines (74 loc) · 1.95 KB
/
CtorByMethodPtr.cpp
File metadata and controls
92 lines (74 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* \file CtorByMethodPtr.cpp
* \brief Ctor - pass by non-static / static method pointer
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
const int threadsNum {3};
//--------------------------------------------------------------------------------------------------
class Worker
{
public:
Worker() = default;
~Worker() = default;
// non-static
void foo() const
{
for (int i = 0; i < ::threadsNum; ++ i) {
std::cout << "[Worker] " << i << std::endl;
}
}
// static
static void staticFoo()
{
for (int i = 0; i < ::threadsNum; ++ i) {
std::cout << "[Worker] Static " << i << std::endl;
}
}
};
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
// by non-static method pointer
{
Worker worker;
std::thread t(&Worker::foo, &worker);
for (int i = 0; i < ::threadsNum; ++ i) {
std::cout << "[Main] - " << i << std::endl;
}
t.join();
std::cout << "[Main] - Exiting" << std::endl;
std::cout << std::endl;
}
// by static method pointer
{
std::thread t(&Worker::staticFoo);
for (int i = 0; i < ::threadsNum; ++ i) {
std::cout << "[Main] - " << i << std::endl;
}
t.join();
std::cout << "[Main] - Exiting" << std::endl;
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
---------------------------------------------
[Main] - 0
[Main] - 1
[Main] - 2
[Worker] 0
[Worker] 1
[Worker] 2
[Main] - Exiting
[Main] - 0
[Main] - 1
[Main] - 2
[Worker] Static 0
[Worker] Static 1
[Worker] Static 2
[Main] - Exiting
---------------------------------------------
#endif