49 C++ 多线程 ------ 多个线程之间共享资源问题。类互斥量(mutex)的概念,及其成员函数 lock() , unlock()

前提,我们要补充一个知识点。再使用类成员函数做为 线程启动的入口,第二个参数可以传递对象 和 对象地址,如下:

类似这样:


thread readthread(&Teacher164::readfunc,tea);

thread readthread(&Teacher164::readfunc,&tea);

那么 这两种 有啥区别?

//当我们在构造一个 thread的时候,如下代码对应 readthread,writethread,
//如果第二个参数传递的是 Teacher164对象 tea,那么会有copy 构造函数的调用。


//如果 我们在传递第二个参数的时候, 传递的是Teacher164对象的tea的地址,相当于多个线程中都会共享这个tea,这才会让在 Teacher164类中的list是同一个,才有共享数据的可能性。

class Teacher164 {

public:
	//共享数据 存在list中
	list<int> msgListQueue;
public:
	//读取 共享数据的线程方法
	void readfunc() {
		int readcount = 0;
		while (true && readcount!=100000) {
			//只要不为空,就可以读取数据
			if (!msgListQueue.empty()) {
				int readvalue = msgListQueue.front();//每次都读取第一个
				cout << "读取到的值为" << readvalue << endl;
				msgListQueue.pop_front();//删除第一个元素
				readcount++;
			}
			else {
				cout << "没有读取到值" << endl;
			}
		}
	}

	//写入 共享数据的线程方法
	void writefunc() {
		for (size_t i = 0; i < 100000; i++)
		{
			msgListQueue.push_back(i);//每次都写到末尾
			cout << "写入元素的值为" << i << endl;
		}
	}

public:
	Teacher164() {
		cout << "Teacher164 构造方法 this = " << this << endl;
	}

	Teacher164(const Teacher164 & obj) {
		cout << "Teacher164 copy 构造方法 this = " << this << "  obj = " << &obj<< endl;
	}

	~Teacher164() {
		cout << "Teacher164 析构函数 this = " << this << endl;
	}
};


//当我们在构造一个 thread的时候,如下代码对应 readthread,writethread,
//如果第二个参数传递的是 Teacher164对象 tea,那么会有copy 构造函数的调用。
//如果 我们在传递第二个参数的时候, 传递的是Teacher164对象的tea的地址,相当于多个线程中都会共享这个tea,这才会让在 Teacher164类中的list是同一个,才有共享数据的可能性。

void main() {
	Teacher164 tea;
	thread readthread(&Teacher164::readfunc,tea);
	thread writethread(&Teacher164::writefunc, tea);

	readthread.join();
	writethread.join();

	//如上,调用的时候,tea是值传递的时候,会调用 copy 构造函数
	//Teacher164 构造方法 this = 00000092A277F508
	//	Teacher164 copy 构造方法 this = 000002713BA6C420  obj = 00000092A277F508
	//	Teacher164 copy 构造方法 this = 000002713BA75310  obj = 00000092A277F508
	//	Teacher164 析构函数 this = 000002713BA6C420
	//	Teacher164 析构函数 this = 000002713BA75310
	//	Teacher164 析构函数 this = 00000092A277F508

	cout << "=========================" << endl;
}

//传递 是地址:  thread readthread1(&Teacher164::readfunc, &tea1); 

结果是:只要使用的 &tea1 的线程,都使用的同一个tea(地址是一样的,没有copy 构造函数调用),这就有可能让其共享数据

void main(){

	Teacher164 tea1;
	thread readthread1(&Teacher164::readfunc, &tea1);
	thread writethread1(&Teacher164::writefunc, &tea1);
	//thread 的构造方法第二个参数 可以是地址,如果是地址,那么 readthread1 和 writethread1的传递就是同一个 teacher

	//  Teacher164 构造方法 this = 000000265F4FFB68
	//	Teacher164 析构函数 this = 000000265F4FFB68

}

对于只读的资源,所有线程都可以一起去读,这是线程安全的。

但是如果有些线程是读资源,有些线程是写资源,那么就要注意线程之间的资源共享问题了。

//我们先把问题简化,假设有2个线程,一个线程 读数据,一个线程写数据。但是运行确一直没有问题。
//于是搞4个线程,2个读数据,2个写数据

有问题的代码

class Teacher164 {

public:
	//共享数据 存在list中
	list<int> msgListQueue;
public:
	//读取 共享数据的线程方法
	void readfunc() {
		int readcount = 0;
		while (true && readcount!=100000) {
			//只要不为空,就可以读取数据
			if (!msgListQueue.empty()) {
				int readvalue = msgListQueue.front();//每次都读取第一个
				cout << "读取到的值为" << readvalue << endl;
				msgListQueue.pop_front();//删除第一个元素
				readcount++;
			}
			else {
				cout << "没有读取到值" << endl;
			}
		}
	}

	//写入 共享数据的线程方法
	void writefunc() {
		for (size_t i = 0; i < 100000; i++)
		{
			msgListQueue.push_back(i);//每次都写到末尾
			cout << "写入元素的值为" << i << endl;
		}
	}

public:
	Teacher164() {
		cout << "Teacher164 构造方法 this = " << this << endl;
	}

	Teacher164(const Teacher164 & obj) {
		cout << "Teacher164 copy 构造方法 this = " << this << "  obj = " << &obj<< endl;
	}

	~Teacher164() {
		cout << "Teacher164 析构函数 this = " << this << endl;
	}
};


void main() {

	cout << "=========================" << endl;

	Teacher164 tea1;
	thread readthread1(&Teacher164::readfunc, &tea1);
	thread writethread1(&Teacher164::writefunc, &tea1);
	//thread 的构造方法第二个参数 可以是地址,如果是地址,那么 readthread1 和 writethread1的传递就是同一个 teacher

	thread readthread2(&Teacher164::readfunc, &tea1);
	thread writethread2(&Teacher164::writefunc, &tea1);

	readthread1.join();
	readthread2.join();
	writethread1.join();
	writethread2.join();

}

fix方案,使用mutex(锁子),结合lock() 和 unlock()方法

mutex - 互斥量的概念

由于要保护共享数据,我们的思路是:在操作时可以将共享数据锁住,其他想操作共享数据的线程必须等待解锁。

mutex是个啥?

是std中的一个类。

std::mutex。。。在标头 <mutex> 定义
class mutex; (C++11 起) 

mutex 需要结合成员函数 lock()和unlock()函数使用。

多个线程尝试用mutex 的 成员函数 lock() 来加锁 这把锁头,只有一个线程能锁定成功。(成功的标志是lock函数返回)。

没有锁定成功的线程会卡在这里,并不断地尝试的锁这把锁头。

锁定成功的线程将数据处理完成后,需要调用unlock()释放锁头。

由于释放了锁头,那么如果还有线程 尝试锁这把锁头,就会去抢。

mutex使用时要小心,lock()后,就应该要unlock().。要注意的是,如果是if等判断语句,一个进口,多个出口的情况,在进口的时候lock了,那么在每一个出口都要注意unlock。

mutex中间保护的数据要合适,主要是保护 共享数据。

        如果共享数据没有保护全,则还是会发生混乱;

        如果除了共享数据,还保护了其他一些不必要的数据,则会影响效率

使用:

mutex mymutex;

mutex mymutex2;

class Teacher165 {

public:
	//共享数据 存在list中
	list<int> msgListQueue;
	mutex mymutex;
	int readcount = 0;//记录已经读取了多少个。
public:
	//读取 共享数据的线程方法
	void readfunc() {
		while (true ) {
			//只要不为空,就可以读取数据
			if (readcount>=200) {
				break;
			}
			mymutex.lock();
			if (!msgListQueue.empty()) {
				int readvalue = msgListQueue.front();//每次都读取第一个
				cout << "读取到的值为" << readvalue << " readcount = " << readcount <<endl;
				msgListQueue.pop_front();//删除第一个元素
				readcount++;
				mymutex.unlock();
			}
			else {
				//cout << "没有读取到值" << endl;
				mymutex.unlock();
			}
		}
	}

	//写入 共享数据的线程方法
	void writefunc() {
		for (size_t i = 0; i < 100; i++)
		{
			mymutex.lock();
			msgListQueue.push_back(i);//每次都写到末尾
			cout << "写入元素的值为" << i << endl;
			mymutex.unlock();
		}
	}

public:
	Teacher165() {
		cout << "Teacher165 构造方法 this = " << this << endl;
	}

	Teacher165(const Teacher165 & obj) {
		cout << "Teacher165 copy 构造方法 this = " << this << "  obj = " << &obj << endl;
	}

	~Teacher165() {
		cout << "Teacher165 析构函数 this = " << this << endl;
	}
};


void main() {

	cout << "=========================" << endl;

	Teacher165 tea1;
	thread readthread1(&Teacher165::readfunc, &tea1);
	thread writethread1(&Teacher165::writefunc, &tea1);
	//thread 的构造方法第二个参数 可以是地址,如果是地址,那么 readthread1 和 writethread1的传递就是同一个 teacher

	thread readthread2(&Teacher165::readfunc, &tea1);
	thread writethread2(&Teacher165::writefunc, &tea1);
	readthread1.join();
	readthread2.join();
	writethread1.join();
	writethread2.join();
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值