-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_Thread.cpp
80 lines (73 loc) · 1.99 KB
/
10_Thread.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
73
74
75
76
77
78
79
80
//
// Created by xuejun on 23-4-25.
//
//(1)线程的创建和使用
//(2)thread 不能拷贝构造,可以移动构造
//(3)join(),detach(),joinable(),thread::hardware_concurrency()
//(4)类成员函数作为线程入口:静态成员函数,非静态成员函数
//(5)call_once(flag,f,args),应用场景:单例模式,懒汉模式
#include<thread>
#include <iostream>
#include <mutex>
#include <functional>
#include <chrono>
#include <mutex>
using namespace std;
mutex mtx;
class Test
{
public:
static void s_print(){cout<<"this is a static func call"<<endl;}
void print(){cout<<"this is a non-static func call"<<endl;}
};
//region 基于callonce实现懒汉模式——>单例模式
once_flag flag; //创建once_flag作为记录调用的标志
class SingleTest
{
public:
// using foo = void(SingleTest);
// function<SingleTest*()> f =[](){return new SingleTest;}
static SingleTest* GetInstance()
{
call_once(flag,[&]()
{
obj = new SingleTest;
cout<<"Instance has been created"<<endl;
}
);
return obj;
}
void Setname(string name)
{
lock_guard<mutex> lk(mtx); //不加锁线程之间访问同一变量引起冲突
this->name = name;
cout<<"This is "<<this->name<<endl;
}
private:
string name;
static SingleTest* obj;
SingleTest(){}; //构造函数必须写出来,否则无法类内创建对象
SingleTest(const SingleTest&)=delete;
SingleTest & operator=(const SingleTest&)=delete;
};
SingleTest* SingleTest::obj = nullptr; //static变量类内声明,类外定义
void myFunc(string name)
{
SingleTest::GetInstance()->Setname(name);
}
//endregion
int main()
{
// thread t1(&Test::s_print);
// Test tt;
// thread t2(&Test::print,&tt);
// t1.join();
// t2.join();
// cout<<thread::hardware_concurrency()<<endl;
thread t1(myFunc,"Tommy");
thread t2(myFunc,"Auther");
thread t3(myFunc,"John");
t1.join();
t2.join();
t3.join();
}