1. 通过函数
thread:标准库的类
join:阻塞主线程并等待
// MultiThread.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<thread>
using namespace std;
void myPrint()
{
cout << "线程开始运行" << endl;
cout << "线程运行结束了" << endl;
}
int main()
{
std::thread my2Obj(myPrint); // 可调用对象
my2Obj.join();// 主线程阻塞在这,并等待myPrint()执行完
cout << "wangtao" << endl;
return 0;
}
detach(): 将主线程和子线程完全分离,子线程会驻留在后台运行,被C++运行时库接管,失去控制
void myPrint()
{
cout << "线程开始运行1" << endl;
cout << "线程开始运行2" << endl;
cout << "线程开始运行3" << endl;
cout << "线程开始运行4" << endl;
cout << "线程开始运行5" << endl;
cout << "线程开始运行6" << endl;
cout << "线程开始运行7" << endl;
cout << "线程开始运行8" << endl;
cout << "线程开始运行9" << endl;
}
int main()
{
std::thread my2Obj(myPrint); // 主线程阻塞在这,并等待myPrint()执行完
my2Obj.detach();
cout << "wangtao1" << endl;
cout << "wangtao2" << endl;
cout << "wangtao3" << endl;
cout << "wangtao4" << endl;
cout << "wangtao5" << endl;
cout << "wangtao6" << endl;
cout << "wangtao7" << endl;
cout << "wangtao8" << endl;
return 0;
}
joinable():判断是否可以成功使用join()或者detach()
程序说明:detach后不能在实施join
int main()
{
std::thread my2Obj(myPrint); // 主线程阻塞在这,并等待myPrint()执行完
if (my2Obj.joinable()){
cout << "1:joinable() == true" << endl;
}
else {
cout << "1:joinable() == false" << endl;
}
my2Obj.detach();
if (my2Obj.joinable()) {
cout << "2:joinable() == true" << endl;
}
else {
cout << "2:joinable() == false" << endl;
}
cout << "wangtao1" << endl;
cout << "wangtao2" << endl;
cout << "wangtao3" << endl;
cout << "wangtao4" << endl;
cout << "wangtao5" << endl;
cout << "wangtao6" << endl;
cout << "wangtao7" << endl;
cout << "wangtao8" << endl;
return 0;
}
int main()
{
std::thread my2Obj(myPrint); // 主线程阻塞在这,并等待myPrint()执行完
if (my2Obj.joinable()){
my2Obj.join();
}
cout << "wangtao1" << endl;
cout << "wangtao2" << endl;
cout << "wangtao3" << endl;
cout << "wangtao4" << endl;
cout << "wangtao5" << endl;
cout << "wangtao6" << endl;
cout << "wangtao7" << endl;
cout << "wangtao8" << endl;
return 0;
}
2.通过类对象创建线程
class CObject
{
public:
void operator ()() {
cout << "线程开始运行" << endl;
cout << "线程结束运行" << endl;
}
};
int main()
{
CObject obj;
std::thread my2Obj(obj); // 主线程阻塞在这,并等待myPrint()执行完
if (my2Obj.joinable()){
my2Obj.join();
}
cout << "see you " << endl;
return 0;
}
class CObject
{
int& m_obj;
public:
CObject(int& i) :m_obj(i) {}
void operator ()() { // 不带参数
cout << "线程开始运行1" << endl;
cout << "线程开始运行2" << endl;
cout << "线程开始运行3" << endl;
cout << "线程开始运行4" << endl;
cout << "线程开始运行5" << endl;
}
};
int main()
{
int i = 6;
CObject obj(i);
std::thread my2Obj(obj); // 主线程阻塞在这,并等待myPrint()执行完
if (my2Obj.joinable()){
my2Obj.detach();
}
cout << "see you " << endl;
return 0;
}
用detach() 主线程结束对象即被销毁,那么子线程的成员函数还能调用吗?
这里的的对象会被复制到子线程中,当主线程结束,复制的子线程对象并不会被销毁
只要是没有引用、指针就不会出现问题
通过复制构造函数和析构函数来验证对象是否复制到了子线程中
// MultiThread.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<thread>
using namespace std;
class CObject
{
int& m_obj;
public:
CObject(int& i) :m_obj(i) {
cout << "ctor" << endl;
}
CObject(const CObject& m) :m_obj(m.m_obj) {
cout << "copy ctor" << endl;
}
~CObject(){
cout << "dtor" << endl;
}
void operator ()() { // 不带参数
cout << "线程开始运行1" << endl;
cout << "线程开始运行2" << endl;
cout << "线程开始运行3" << endl;
cout << "线程开始运行4" << endl;
cout << "线程开始运行5" << endl;
}
};
int main()
{
int i = 6;
CObject obj(i);
std::thread my2Obj(obj); // 主线程阻塞在这,并等待myPrint()执行完
if (my2Obj.joinable()){
my2Obj.detach();
}
cout << "see you " << endl;
return 0;
}
子线程的析构函数在后台执行,所以输出的dtor是主线程的。用join() 结果为:
3.通过lambda表达式创建线程
int main()
{
auto myLamThread = [] {
cout << "线程开始运行" << endl;
cout << "线程结束运行" << endl;
};
thread cthread(myLamThread);
cthread.join();
std::cout << "see you " << endl;
return 0;
}