参考以下博文: [c++11]多线程编程(一)——初识 [c++11]多线程编程(二)——理解线程类的构造函数 [c++11]多线程编程(三)——竞争条件与互斥锁 [c++11]多线程编程(四)——死锁(Dead Lock) [c++11]多线程编程(五)——unique_lock [c++11]多线程编程(六)——条件变量(Condition Variable) 深入学习c+±-多线程编程(三)thread的两种死法
以下代码笔记:
一、线程初始初始方式
#include <iostream>
#include <thread>
#include <string>
using namespace std
;
void printALL(int a
,int b
,int c
){
cout
<<a
<<" "<<b
<<" "<<c
<<endl
;
}
void add(int a
,int b
,int& c
){
c
= a
+b
;
}
void printString(const string
& info
,const string
& info2
){
cout
<<"hello "<<info
<<" "<<info2
<<endl
;
}
void testThreadInit(){
int a
=3;
int b
=4;
int c
=5;
thread
t([=](){
printALL(a
,b
,c
);
});
t
.join();
thread
t2(printALL
,a
,b
,c
);
t2
.join();
thread
t3([=,&c
](){
add(a
,b
,c
);
});
t3
.join();
cout
<<"after add: "<<c
<<endl
;
c
=0;
thread
t4(add
,a
,b
,ref(c
));
t4
.join();
cout
<<"after add: "<<c
<<endl
;
string
abc("abc");
string
def("def");
thread
t5([&](){
printString(abc
,def
);
});
t5
.join();
thread
t6(printString
,abc
,def
);
t6
.join();
thread
t7(printString
,cref(abc
),cref(def
));
t7
.join();
}
int main(){
testThreadInit();
return 0;
}
执行结果:
3 4 5
3 4 5
after add
: 7
after add
: 7
hello abc def
hello abc def
hello abc def
二、thread的两种死法
#include <iostream>
#include <thread>
#include <chrono>
using namespace std
;
class Obj{
public:
Obj(){
cout
<<"hello\n";
}
~Obj(){
cout
<<"world\n";
}
};
void joinWorker(){
}
void detachWorker(){
Obj obj
;
this_thread
::sleep_for(chrono
::seconds(1));
}
* detach适合不会出错,生命周期比整个程序短,不想管理的程序
* 缺点:
* 如果程序执行时间长,则可能不会调用析构函数
*/
int main(){
thread
j(joinWorker
);
thread
w(detachWorker
);
w
.detach();
if(j
.joinable())
j
.join();
return 0;
}
class ThreadGuard{
public:
ThreadGuard(thread
& t
): m_thread(t
){
}
~ThreadGuard(){
if(m_thread
.joinable())
m_thread
.join();
}
private:
thread
& m_thread
;
};
int main(){
Obj obj
;
thread
j(joinWorker
);
ThreadGuard
t(j
);
return 0;
}
int main(){
Obj obj
;
thread
j(joinWorker
);
return 0;
}
#include <iostream>
#include <functional>
using namespace std
;
struct Print
{
void operator()(int a
,int b
,int c
) const{
cout
<<"print 1:"<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
}
};
void printInfo(int a
,int b
,int c
){
cout
<<"print 2:"<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
}
template <typename T1
,typename T2
,typename T3
>
void templatePrint(T1 a
,T2 b
,T3 c
){
cout
<<"print 3:"<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
}
struct TemplatePrint
{
template<typename T1
,typename T2
,typename T3
>
void operator() (T1 a
,T2 b
,T3 c
)const{
cout
<<"print 4:"<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
}
};
template <typename Func
>
void printUserFunc(Func func
,int a
,int b
,int c
){
func(a
,b
,c
);
}
int main(){
Print printUserClass
;
TemplatePrint printUserTempClass
;
printInfo(1,2,3);
templatePrint(11,22,33);
printUserClass(111,222,333);
printUserTempClass(1111,2222,3333);
auto local
= [](int a
,int b
,int c
){
cout
<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
};
local(123,123,123);
printUserFunc(local
,121,212,313);
printUserFunc([](int a
,int b
,int c
){
cout
<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
},121,212,313);
int a
=23,b
=34,c
=45;
auto local2
= [a
,b
,c
](){
cout
<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
};
local2();
auto local3
= [&](){
cout
<<"a "<<a
<<" b "<<b
<<" c "<<c
<<endl
;
};
local3();
return 0;
}
三、几种锁的方式
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include <fstream>
using namespace std
;
std
::mutex mu
;
class LogFile{
mutex _mu
;
ofstream f
;
public:
LogFile(){
f
.open("log.txt");
}
~LogFile(){
f
.close();
}
void shared_print(string msg
,int id
){
2 使用unique_lock defer_lock设置初始化的时候不进行默认的上锁操作
unique_lock
<mutex
> guard(_mu
,defer_lock
);
guard
.lock();
guard
.unlock();
guard
.lock();
f
<<msg
<<id
<<endl
;
cout
<<msg
<<id
<<endl
;
1 使用unique_lock提供的
lock()和
unlock()接口,guard声明的时候默认为上锁的状态
unique_lock
<mutex
> guard(_mu
);
cout
<<"1 do something here..."<<endl
;
guard
.unlock();
cout
<<"2 do something here..."<<endl
;
guard
.lock();
f
<<msg
<<id
<<endl
;
cout
<<msg
<<id
<<endl
;
guard
.unlock();
0 直接使用guard进行保护所操作
lock_guard
<mutex
> guard(mu
);
f
<<msg
<<id
<<endl
;
}
};
void function_1(LogFile
& log
){
for(int i
=0;i
>-100;i
--){
log
.shared_print(string("From t1: "),i
);
}
}
int main(){
LogFile log
;
thread
t1(function_1
,ref(log
));
for(int i
=0;i
<100;i
++){
log
.shared_print(string("From main: "),i
);
}
t1
.join();
return 0;
}
void shared_print(string msg
,int id
){
std
::lock_guard
<std
::mutex
> guard(mu
);
mu
.lock();
cout
<<msg
<<id
<<endl
;
mu
.unlock();
}
普通函数 无参
void function_1() {
for(int i
=0; i
>-100; i
--)
shared_print(string("From t1:"),i
);
}
int main()
{
std
::thread
t1(function_1
);
for(int i
=0; i
<100; i
++)
shared_print(string("From t2:"),i
);
t1
.join();
return 0;
}
#include <thread>
#include <deque>
#include <mutex>
#include <condition_variable>
using namespace std
;
deque
<int> q
;
mutex mu
;
condition_variable cond
;
void function_1(){
int count
=10;
while(count
>0){
unique_lock
<mutex
> locker(mu
);
q
.push_front(count
);
locker
.unlock();
cond
.notify_one();
this_thread
::sleep_for(chrono
::seconds(1));
count
--;
}
}
void function_2(){
int data
= 0;
while(data
!=1){
unique_lock
<mutex
> locker(mu
);
while(q
.empty())
cond
.wait(locker
);
data
= q
.back();
q
.pop_back();
locker
.unlock();
cout
<<"t2 got a value from t1: "<<data
<<endl
;
}
}
int main(){
thread
t1(function_1
);
thread
t2(function_2
);
t1
.join();
t2
.join();
return 0;
}