什么是多线程?我们可以理解为一个线程执行一个代码段,所以多个线程就是执行多个代码段,如果当一个线程结束后,进程就退出了,这个线程我们称之为主线程。每个进程可以有一个或一个以上的线程,但只有一个主线程,我们暂且理解主线程为main函数。在多核CPU中,多线程的程序比单线程的运行效率高,因此开启线程的个数,理想情况下是根据CPU的核数来定。
linux查看线程的命令:
linux查看线程的命令:
root@epc:~# pstree -p | grep test
|-sshd(841)-+-sshd(896)---bash(905)---test(1382)-+-{test}(1383)
| | `-{test}(1384)
root@epc:~#
C++11中std命名空间将Boost库中的Thread引入,在C++11多线程的库是pthread,它的头文件是thread。
本博客的内容目录如下:
一.调用thread有多种场景:
1.普通函数调用(有参和无参)
2.lambda函数调用
3.类对象调用
4.类成员函数调用
二.join和detach的区别
join和detach函数的作用是运行线程。以join运行的线程会阻塞主线程,主线程会等待其他子线程运行完毕,才继续执行;而以detach运行的线程会和主线程独立开来,主线程正常退出后程序退出,系统会回收所有的资源即关闭所有子线程,因此为了解决detach带来的不确定性,需要主线程永远确保在所有线程关闭后才关闭,即成为守护线程,以确保detach能够顺利执行。
join和detach不能同时调用,在win系统会报异常,在linux虽然能正常运行,但也会带来不确定的后果;
三.基本函数
1.获取线程ID
std::this_thread::get_id()
2.joinable
线程对象只能执行一次join或者detach,我们可以通过joinable函数来查看是否已经被调用过。
一.调用thread有多种场景
//普通函数调用(有参和无参)
#include <iostream>
#include <thread>
using namespace std;
void func()
{
cout << "子线程结束" << endl;
}
void funcA(int a)
{
cout << "子线程结束" << endl;
}
int main()
{
thread th1(func);
thread th2(funcA, 100);
th1.join();
th2.join();
cout << "主线程结束" << endl;
return 0;
}
//打印结果
root@epc:/home/share/test# g++ -o test test.cpp -std=c++11 -lpthread
root@epc:/home/share/test# ./test
子线程结束
子线程结束
主线程结束
root@epc:/home/share/test#
//lambda函数调用
#include <iostream>
#include <thread>
#include <string>
using namespace std;
int main()
{
string str = "lambda函数";
auto func = [&](int a, int b)->int{
cout << str << endl;
cout << "sum=" << (a+b) << endl;;
cout << "子线程结束" << endl;
};
thread th(func, 100, 200);
th.join();
cout << "主线程结束" << endl;
return 0;
}
//打印结果
root@epc:/home/share/test# ./test
lambda函数
sum=300
子线程结束
主线程结束
root@epc:/home/share/test#
//类成员函数调用
//单例模式
#include <iostream>
#include <thread>
using namespace std;
class SingleTon
{
public:
~SingleTon(){}
static SingleTon& getInstance()
{
static SingleTon instance;
return instance;
}
static void createThread()
{
thread th(bind(&SingleTon::display, &SingleTon::getInstance()));
th.join();
}
void display()
{
cout << "子线程结束" << endl;
}
private:
SingleTon(){}
};
int main()
{
SingleTon::createThread();
SingleTon::createThread();
cout << "主线程结束" << endl;
return 0;
}
//打印结果
root@epc:/home/share/test# ./test
子线程结束
子线程结束
主线程结束
root@epc:/home/share/test#
//类对象调用
#include <iostream>
#include <thread>
using namespace std;
class Test
{
public:
void display()
{
cout << "子线程结束" << endl;
}
};
int main()
{
Test t;
thread th(bind(&Test::display, &t));
th.join();
cout << "主线程结束" << endl;
return 0;
}
//打印结果
root@epc:/home/share/test# ./test
子线程结束
主线程结束
root@epc:/home/share/test#
二.join和detach的区别
//以join方式运行的线程
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
auto func = []{
cout << "子线程开始,等待10s" << endl;
this_thread::sleep_for(chrono::seconds(10));
cout << "子线程结束" << endl;
};
thread th1(func);
thread th2(func);
th1.join();
th2.join();
cout << "主线程结束" << endl;
return 0;
}
//打印结果
root@epc:/home/share/test# ./test
子线程开始,等待10s
子线程开始,等待10s
子线程结束
子线程结束
主线程结束
root@epc:/home/share/test# cd /home/share/test
//以detach方式运行的线程
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
auto func = []{
cout << "子线程开始,等待5s" << endl;
this_thread::sleep_for(chrono::seconds(5));
cout << "子线程结束" << endl;
};
thread th1(func);
thread th2(func);
th1.detach();
th2.detach();
cout << "主线程开始,等待10s" << endl;
this_thread::sleep_for(chrono::seconds(10));
cout << "主线程结束" << endl;
return 0;
}
//打印结果
root@epc:/home/share/test# ./test
主线程开始,等待10s
子线程开始,等待5s
子线程开始,等待5s
子线程结束
子线程结束
主线程结束
root@epc:/home/share/test#
三.基本函数
//joinenable
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
auto func = []{
cout << "子线程开始,等待5s" << endl;
cout << "子线程结束" << endl;
};
thread th1(func);
thread th2(func);
cout << "join执行前joinable=" << th1.joinable() << endl;
cout << "detach执行前joinable=" << th2.joinable() << endl;
th1.join();
th2.detach();
cout << "join执行后joinable=" << th1.joinable() << endl;
cout << "detach执行后joinable=" << th2.joinable() << endl;
cout << "主线程结束" << endl;
return 0;
}
//打印结果
root@epc:/home/share/test# ./test
join执行前joinable=1
detach执行前joinable=1
子线程开始,等待5s
子线程结束
子线程开始,等待5s
子线程结束
join执行后joinable=0
detach执行后joinable=0
主线程结束
root@epc:/home/share/test#
*/