-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadTest.cpp
More file actions
73 lines (68 loc) · 1.56 KB
/
Copy paththreadTest.cpp
File metadata and controls
73 lines (68 loc) · 1.56 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
//
// Created by william on 2021/7/17.
//
#include "base.h"
#include "commonMacro.h"
namespace threadTest
{
/// 此demo用于解决在多线程下,因为悬垂指针导致的崩溃问题
class ThreadTestCheck
{
public:
~ThreadTestCheck()
{
std::weak_ptr<int> isLife = m_checkIsLife;
m_checkIsLife = nullptr;
int n = 0;
while (!isLife.expired())
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (++n > 1000)
{
break;
}
}
}
std::shared_ptr<int>& isLive() { return m_checkIsLife; }
private:
std::shared_ptr<int> m_checkIsLife = std::make_shared<int>();
};
struct ThreadTest : public ThreadTestCheck
{
// operator=, returns a reference to this
~ThreadTest()
{
std::cout << "~ThreadTest called" << std::endl;
}
std::string& str()
{
return m_str;
}
void setStr(std::string s)
{
m_str = std::move(s);
}
private:
std::string m_str = "null";
};
void threadTest()
{
auto* test = new ThreadTest();
auto str = test->str();
LOG_INFO("%s", str.c_str());
delete test;
std::weak_ptr<int> isLivePtr = std::weak_ptr<int>(test->isLive());
auto isLive = isLivePtr.lock();
if (isLive)
{
LOG_INFO("is live");
}
else
{
LOG_ERROR("is not live");
}
/// 此时,test是一个悬垂指针,使用这个指针可能会崩溃也可能不会崩溃
test->setStr("after");
LOG_ERROR(test->str().c_str());
}
} // namespace threadTest