-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_weak_ptr.cpp
72 lines (59 loc) · 1.62 KB
/
shared_weak_ptr.cpp
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
#include <iostream>
#include <thread>
#include <unistd.h>
#include <string>
#include <sstream>
#include <map>
using namespace std;
class VarStore {
public:
VarStore(int value) : var_(value) {}
void SetVar(int value) { var_ = value; }
int GetVar() { return var_; }
private:
int var_{0};
};
//void thread_function_1(VarStore* obj_ptr, int sleep_time) {
void thread_function_1(shared_ptr<VarStore> ptr, int sleep_time) {
stringstream msg;
msg << "sssleeping for " << sleep_time << " seconds" << endl;
cout << msg.str();
sleep(sleep_time);
//delete ptr.get();
//cout << "sptr usage cnt is:" << sptr.use_count() << endl;
ptr.reset();
cout << "deleted shared ptr" << endl;
}
void thread_function_2(const shared_ptr<VarStore>& ptr, int sleep_time) {
stringstream msg;
msg << "sptr usage cnt is:" << ptr.use_count() << endl;
cout << msg.str();
msg << "sleeping for " << sleep_time << " seconds" << endl;
cout << msg.str();
sleep(sleep_time);
cout << "sptr usage cnt is:" << ptr.use_count() << endl;
if (ptr.use_count())
cout << "shouldnt be here" << endl;
else
cout << "nice" << endl;
#if 0
weak_ptr<VarStore> wptr = sptr;
if (auto sp = wptr.lock())
cout << "sptr usage cnt is:" << sp.use_count() << endl;
else
cout << "do nothing" << endl;
// sptr.reset();
#endif
}
int main() {
//VarStore* obj = new VarStore(1);
//shared_ptr<VarStore> sptr = obj;
shared_ptr<VarStore> sptr = make_shared<VarStore>(1);
auto obj1 = sptr.get();
cout << "its:" << obj1->GetVar() << endl;
thread t2(thread_function_2, sptr, 2);
thread t1(thread_function_1, std::move(sptr), 1);
t1.join();
t2.join();
return 0;
}