C++多线程1(复习向笔记)

创建线程以及相关函数

当用thread类创建线程对象绑定函数后,该线程在主线程执行时就已经自动开始执行了,join起到阻塞主线程的作用

#include <iostream>
#include <thread>
#include <string>
using namespace std;
//测试函数
void printString(string str)
{
	cout << str << endl;
}
int main()
{
	//创建线程,需要包括thread头文件,构造函数后续参数里可选,放入线程绑定函数的参数
	thread thread1(printString,"Hello World!");
	//joinable函数,有些线程不一定能用join或者detach,joinable返回一个布尔值,true时使用join增加严谨性
	bool flag = thread1.joinable();
	if (flag)
		thread1.join();//主程序等待线程执行完毕,不加join可能会导致线程绑定的函数没执行完,主程序就结束了

	//分离主线程和子线程,会可能导致子线程还未执行完毕,主线程就结束了
	/*thread1.detach();*/
	return 0;
}

使用detach输出结果,一般使用较少
在这里插入图片描述
更直观的join和joinable的例子

#include <iostream>
#include <thread>
using namespace std;

void display()
{
	for (int i = 0; i < 100; i++)
		cout << i << " ";
	cout << endl << "子线程结束" << endl;
}

int main()
{
	thread thread1(display);
	bool flag = thread1.joinable();

	if (flag)
		thread1.join();
	cout << "主线程结束" << endl;
	return 0;
}

输出
不难发现,主线程阻塞在了join中,等待子线程执行完毕,再往下执行

常见问题以及解决方法

一般注意变量的声明周期即可

绑定函数的参数为引用

使用ref()函数

#include <iostream>
#include <thread>
using namespace std;

void test01(int& a)
{
	a += 1;
}
int main()
{
	int a = 11;
	//绑定函数时参数,传递引用时需要用std下的ref函数将变量转换为引用类型,否则thread无法判断为引用类型
	thread thread1(test01, ref(a));
	if (thread1.joinable())
		thread1.join();
	cout << a << endl;
	return 0;
}

在这里插入图片描述
错误例子,运行结果报错,也有可能不报错
错误原因,在本例中,a不在main中,函数执行完后内存会释放掉,由于是线程模式,test01和test02的执行速度不一致,有可能在test02执行完后,test01还没有执行完,所以导致了空指针异常!
解决方法:将a变成局部变量

#include <iostream>
#include <thread>
using namespace std;
thread thread1;
void test01(int& a)
{
	a += 1;
}

void test02()
{
	int a = 11;
	//绑定函数时参数,传递引用时需要用std下的ref函数将变量转换为引用类型,否则thread无法判断为引用类型
	thread1 = thread(test01, ref(a));
}
int main()
{
	
	test02();
	if (thread1.joinable())
		thread1.join();
	return 0;
}

指针提前释放

//指针案例
void test03(int* a)
{
	cout << "*a = " << * a << endl;
}
int main()
{
	int* p = new int(1);
	thread thread1(test03,p);
	//在join之前手动释放了指针,指针指向的空间值变成了随机值
	delete p;
	if (thread1.joinable())
		thread1.join();
	return 0;
}

在这里插入图片描述

类成员函数作为线程函数,但对象被提前释放

其实跟指针的报错差不多,注意在join()之前不要释放对象即可
智能指针可以减少在join()之前手动释放的问题,它会自动调用析构函数自己销毁对象
整体源代码

#include <iostream>
#include <thread>
#include <memory>
using namespace std;
//引用案例
void test01(int& a)
{
	a += 1;
}
//指针案例
void test03(int* a)
{
	cout << "*a = " << * a << endl;
}
//类案例
class A {
public:
	void func()
	{
		cout << "类成员函数作为线程函数" << endl;
	}
};
int main()
{
	A a;
	//类成员函数作为线程函数时,需要用&来制定成员函数的地址,同时还需要传递指针/引用
	thread thread1(&A::func,&a);
	thread1.join();
	//智能指针,已经是指针了
	shared_ptr<A> b = make_shared<A>();
	//直接传入值即可
	thread thread2(&A::func, b);
	thread2.join();
	return 0;
}

在这里插入图片描述

互斥锁解决变量访问互质问题

学过OS这部分就很容易理解了
只是C++具体实现罢了,具体看代码
线程安全:如果多线程程序每一次运行的结果跟单线程程序运行的结果是一样的,那么就称这个线程是安全的

#include <iostream>
#include <thread>
#include<mutex>
using namespace std;
mutex mtx;
int a = 0;
void add()
{
	for (int i = 0; i < 1000; i++)
	{
		//上锁相当于P(mutex)
		mtx.lock();
		a++;
		//解锁相当于V(mutex)
		mtx.unlock();
	}
		
}
int main()
{
	//创建两个线程
	thread thread1(add);
	thread thread2(add);

	thread1.join();
	thread2.join();
	//输出共享变量
	cout << a << endl;
	return 0;
}
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值