有关多线程争夺互斥量问题
作者:zizbee
链接:https://zhuanlan.zhihu.com/p/194198073
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
mutex m;//实例化m对象,不要理解为定义变量
void proc1(int a)
{
m.lock();
cout << "proc1函数正在改写a" << endl;
cout << "原始a为" << a << endl;
cout << "现在a为" << a + 2 << endl;
m.unlock();
}
void proc2(int a)
{
m.lock();
cout << "proc2函数正在改写a" << endl;
cout << "原始a为" << a << endl;
cout << "现在a为" << a + 1 << endl;
m.unlock();
}
int main()
{
int a = 0;
thread t1(proc1, a);
thread t2(proc2, a);
t1.join();
t2.join();
return 0;
}
- 如果该互斥量当前未上锁,则本线程将该互斥量锁住,直到调用unlock()之前,本线程一直拥有该锁。
- 如果该互斥量当前被其他线程锁住,则本线程被阻塞,直至该互斥量被其他线程解锁,此时本线程将该互斥量锁住,直到调用unlock()之前,本线程一直拥有该锁。
有关条件变量相关问题
condition_variable
std::condition_variable类搭配std::mutex类来使用,
std::condition_variable对象(std::condition_variable cond;)的作用不是用来管理互斥量的,它的作用是用来同步线程,它的用法相当于编程中常见的flag标志(A、B两个人约定flag=true为行动号角,默认flag为false,A不断的检查flag的值,只要B将flag修改为true,A就开始行动)。
类比到std::condition_variable,A、B两个人约定notify_one为行动号角,A就等着(调用wait(),阻塞),只要B一调用notify_one,A就开始行动(不再阻塞)。
wait
wait函数需要传入一个std::mutex(一般会传入std::unique_lock对象),即上述的locker。
wait函数会自动调用 locker.unlock() 释放锁(因为需要释放锁,所以要传入mutex)并阻塞当前线程,本线程释放锁使得其他的线程得以继续竞争锁。一旦当前线程获得notify(通常是另外某个线程调用 notify_* 唤醒了当前线程),wait() 函数此时再自动调用 locker.lock()上锁。
cond.notify_one(): 随机唤醒一个等待的线程
cond.notify_all(): 唤醒所有等待的线程
生产者与消费者模型
#include <queue>
#include <thread>
#include <mutex>
#include <vector>
#include <iostream>
#include <condition_variable>
using namespace std;
//生产者消费者模型
struct Product {
int id; //商品id
string data; //商品信息
};
mutex mx; //互斥量
condition_variable consumerCond;
condition_variable producerCond;
queue<Product> q; //商品仓库
int maxSize = 3;
int id = 1; //商品全局Id
void consumerActor() {
unique_lock<mutex> consumerLock(mx);
cout << "[" << this_thread::get_id() << "]" << "获取了互斥锁" << endl;
while (q.empty()) {
cout << "[" << this_thread::get_id() << "]" << "释放了锁" << endl;
consumerCond.wait(consumerLock); //wait会导致当前线程阻塞在这里,同时释放互斥锁,当被唤醒时会对互斥锁重新枷锁
cout << "[" << this_thread::get_id() << "]" << "重新获取了锁" << endl;
}
Product tmp = q.front(); q.pop();
cout << "[" << this_thread::get_id() << "]" << "-" << tmp.id << tmp.data << endl;
producerCond.notify_one();
cout << "[" << this_thread::get_id() << "]" << "释放了锁" << endl;
}
void producerActor() {
unique_lock<mutex> producerLock(mx);
cout << "[" << this_thread::get_id() << "]" << "获取了互斥锁" << endl;
while (q.size() >= maxSize) {
cout << "[" << this_thread::get_id() << "]" << "释放了锁" << endl;
producerCond.wait(producerLock);
cout << "[" << this_thread::get_id() << "]" << "重新获取了锁" << endl;
}
Product tmp;
tmp.id = id++;
tmp.data = "***";
q.push(tmp);
cout << "[" << this_thread::get_id() << "]" << "+" << tmp.id << tmp.data << endl;
consumerCond.notify_one();
cout << "[" << this_thread::get_id() << "]" << "释放了锁" << endl;
}
void consumer() {
while (1) {
consumerActor();
}
}
void producer() {
while (1) {
producerActor();
}
}
void helper(int consumerNum, int producerNum) {
vector<thread> consumerList;
for (int i = 0; i < consumerNum; i++) {
consumerList.push_back(thread(consumer));
}
vector<thread> producerList;
for (int i = 0; i < producerNum; i++) {
producerList.push_back(thread(producer));
}
for (int i = 0; i < consumerNum; i++) {
consumerList[i].join();
}
for (int i = 0; i < producerNum; i++) {
producerList[i].join();
}
}
int main() {
helper(3, 8);
return 0;
}