1.创建一个线程
创建线程比较简单,C++提供头文件thread,使用std的thread实例化一个线程对象创建,示例:
#include <iostream>
#include <thread>
using namespace std;
void t1() //普通的函数,用来执行线程
{
for (int i = 0; i < 20; ++i)
{
cout << "t1111\n";
}
}
void t2()
{
for (int i = 0; i < 20; ++i)
{
cout << "t22222\n";
}
}
int main()
{
thread th1(t1); //实例化一个线程对象th1,使用函数t1构造,然后该线程就开始执行了(t1())
thread th2(t2);
cout << "here is main\n\n";
return 0;
}
不过这个示例是有问题的,因为在创建了线程后线程开始执行后,主线程main()不会停止,会继续执行线程之后的语句cout << "here is main\n\n";
,main执行完以后退出,此时线程对象还是joinable的,线程仍然存在但指向它的线程对象在mian中的语句执行结束后被销毁,所以会抛出异常。
那么该如何保证子线程执行完了退出后再退出主线程呢?
2.thread::join()
使用join接口可以解决上述问题,join的作用是让主线程等待直到该子线程执行结束,示例:
#include <iostream>
#include <thread>
using namespace std;
void t1()
{
for (int i = 0; i < 20; ++i)
{
cout << "t1111\n";
}
}
void t2()
{
for (int i = 0; i < 20; ++i)
{
cout << "t22222\n";
}
}
int main()
{
thread th1(t1);
thread th2(t2);
th1.join(); //等待th1执行完
th2.join(); //等待th2执行完
cout << "here is main\n\n";
return 0;
}
此时就可以正常地执行子线程了,同时注意最后一个输出,说明了main是等待子线程结束才继续执行的
需要注意的是线程对象执行了join后就不再joinable了,所以只能调用join一次。
3.mutex
头文件是,mutex是用来保证线程同步的,防止不同的线程同时操作同一个共享数据。
int cnt = 20;
mutex m;
void t1()
{
while (cnt > 0)
{
m.lock();
if (cnt > 0)
{
--cnt;
cout << cnt << endl;
}
m.unlock();
}
}
void t2()
{
while (cnt > 0)
{
m.lock();
if (cnt > 0)
{
--cnt;
cout << cnt << endl;
}
m.unlock();
}
}
int main()
{
thread th1(t1);
thread th2(t2);
th1.join();
th2.join();
return 0;
}
运行结果,cnt是依次递减的,没有因为多线程而打乱次序:
但是使用mutex是不安全的,当一个线程在解锁之前异常退出了,那么其它被阻塞的线程就无法继续下去。
4.std::lock_guard
使用lock_guard则相对安全,它是基于作用域的,能够自解锁,当该对象创建时,它会像m.lock()一样获得互斥锁,当生命周期结束时,它会自动析构(unlock),不会因为某个线程异常退出而影响其他线程。示例:
int cnt = 20;
mutex m;
void t1()
{
while (cnt > 0)
{
lock_guard<mutex> lockGuard(m);
if (cnt > 0)
{
--cnt;
cout << cnt << endl;
}
}
}
void t2()
{
while (cnt > 0)
{
lock_guard<mutex> lockGuard(m);
if (cnt > 0)
{
--cnt;
cout << cnt << endl;
}
}
}
参考网址
【C++:线程(std::thread)】https://www.cnblogs.com/whlook/p/6573659.html