-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstd.thread.cpp
More file actions
executable file
·154 lines (122 loc) · 3.49 KB
/
std.thread.cpp
File metadata and controls
executable file
·154 lines (122 loc) · 3.49 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* =====================================================================================
*
* Filename: std.thread.cpp
*
* Description:
*
* Version: 1.0
* Created: 03/26/2019 13:37:54
* Revfision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <thread>
#include "iostream"
#include <functional>
#include <chrono>
#include <vector>
#include <mutex>
class TestThread {
public:
std::mutex *_mutx;
int volatile x;
TestThread(int _x):x(_x){
_mutx = new std::mutex();
printf("constructor is called\n");
};
~TestThread() {
printf("destroy...\n");
if (_mutx!=nullptr) {
delete _mutx; _mutx = nullptr;
}
}
void async_display() {
std::function<void(void)> do_display = [this](void) {
_mutx->lock();
printf("value of this->x = %d\n", x);
x = x+1;
_mutx->unlock();
};
std::thread t(do_display);
t.detach();
// t.join();
}
void display(int i=0) {
printf("value of x = %d\n", x);
if (i==0) return;
printf("value of i = %d\n", i);
}
};
class X {
public:
char * _x = NULL;
X() = delete;
X(const X& x) {
printf("x.copy constructor\n");
if (x._x==NULL) { return; }
size_t size = strlen(x._x);
_x = new char[size + 1];
memcpy(_x, x._x, size);
}
X(const char *x) {
printf("constructor is call...\n");
if (x==NULL) { return; }
size_t size = strlen(x);
_x = new char[size + 1];
memcpy(_x, x, size);
}
X(X &&x):_x(x._x) {
x._x = nullptr;
printf("move copy constructor is call\n");
}
~X() {
printf("destory x\n");
if (_x!=NULL) {
printf("call delete[] _x;\n");
delete[] _x;
_x = NULL;
}
};
void display() { printf("value of x:%s\n", _x); };
};
int main ( int argc, char *argv[] )
{
X x("hello world");
x.display();
std::function<void(void)> xf = std::bind(&X::display, x);
return 0;
xf();
std::this_thread::sleep_for(std::chrono::seconds(2));
std::mutex *tmp_mutx = new std::mutex();
delete tmp_mutx;
TestThread th_inst = {0};
th_inst.display();
std::vector<int> vec = {1,2,3,4,5};
// for ( int x : vec ) { th_inst.async_display(); }
for ( int x=0; x<1; x++) { th_inst.async_display(); }
printf("all displayed...........\n");
std::this_thread::sleep_for(std::chrono::seconds(2));
printf("....................\n");
std::function<void(int)> f = std::bind(&TestThread::display, th_inst, std::placeholders::_1);
// std::function<void(int)> f_ref = std::bind(&TestThread::display, std::ref(th_inst), std::placeholders::_1);
#if 0
std::thread thread_class(f, 10000);
thread_class.detach();
std::thread thread_class_(&TestThread::display, th_inst, 3);
thread_class_.detach();
std::function<void(void)> f_void = std::bind(&TestThread::display, th_inst, 5);
std::thread thread_class_0(f_void);
thread_class_0.detach();
printf("....................\n");
th_inst.display();
std::this_thread::sleep_for(std::chrono::seconds(2));
std::this_thread::sleep_for(std::chrono::seconds(2));
std::this_thread::sleep_for(std::chrono::seconds(2));
#endif
return EXIT_SUCCESS;
}