c++多线程thread操作(四)死锁的问题和解决

c++多线程系列

c++多线程thread操作(一)环境部署

c++多线程thread操作(二)线程管理

c++多线程thread操作(三)数据竞争互斥访问

c++多线程thread操作(四)死锁的问题和解决

c++多线程thread操作(五)unique_lock加锁

c++多线程thread操作(六)条件变量的使用

c++多线程thread操作(七)父进程获取子进程变量的结果

c++多线程thread操作(八)父线程传递数据给子线程

c++多线程thread操作(九)可调用方式

(终)c++多线程thread操作(十)多线程并行实现数据相加的和

1. 死锁问题的出现,出现了循环调用,并且抢占了资源。

class LofFile {
private:
	mutex m_mutex1;
	mutex m_mutex2;
public:
	LofFile() {}
	void shared_print(string msg, int id) {
		lock_guard<mutex> guard1(m_mutex1);
		lock_guard<mutex> guard2(m_mutex2);
		cout << "--1--From" << id << " : " << msg << endl;
	}
	void shared_print2(string msg, int id) {
		lock_guard<mutex> guard2(m_mutex2); 
		lock_guard<mutex> guard1(m_mutex1);
		cout << "--2--From" << id << " : " << msg << endl;
	}

};
void func_1(LofFile&log) {  //配合3
	for (int i = 0; i > -100; i--) { //写1000就顺序执行,写100就死锁
		log.shared_print("From t1: ", i); // cout是可竞争资源
	}
}
int main() {
	LofFile log;
	thread t1(func_1,ref(log)); 
	for (int i = 0; i < 100; i++) {
		log.shared_print2("From main: ", i);
	}
	t1.join();
	return 0;
}

上述代码shared_print锁住mutex1,shared_print锁住mutex2,二者不释放自己锁的情况下请求访问其它资源;

2. 解决方案:

(1)手动将shared_print2中guard_lock锁的顺序定义为和1一样:

void shared_print2(string msg, int id) {
	lock_guard<mutex> guard2(m_mutex1); // 交换2者顺序
	lock_guard<mutex> guard1(m_mutex2);
	cout << "--2--From" << id << " : " << msg << endl;
}

(2)利用库函数自动设定顺序:

void shared_print(string msg, int id) {
	//2. Lock固定顺序,需要在guard后面添加adopt_lock
	lock(m_mutex1, m_mutex2);
	lock_guard<mutex> guard1(m_mutex1,adopt_lock);
	lock_guard<mutex> guard2(m_mutex2, adopt_lock);
	cout << "--1--From" << id << " : " << msg << endl;
}
void shared_print2(string msg, int id) {
	lock(m_mutex1, m_mutex2);
	lock_guard<mutex> guard2(m_mutex2, adopt_lock); 
	lock_guard<mutex> guard1(m_mutex1, adopt_lock);
	cout << "--2--From" << id << " : " << msg << endl;
}

lock 函数可以指定锁的顺序,需要在lock_guard下添加adopt_lock形参;

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Linux多线程服务端编程:使用muduo C++网络库》主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread。 目 录 第1部分C++ 多线程系统编程 第1章线程安全的对象生命期管理3 1.1当析构函数遇到多线程. . . . . . . . . . . . . . . . .. . . . . . . . . . . 3 1.1.1线程安全的定义. . . . . . . . . . . . . . . . .. . . . . . . . . . . 4 1.1.2MutexLock 与MutexLockGuard. . . . . . . . . . . . . . . . . . . . 4 1.1.3一个线程安全的Counter 示例.. . . . . . . . . . . . . . . . . . . 4 1.2对象的创建很简单. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 5 1.3销毁太难. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 7 1.3.1mutex 不是办法. . . . . . . . . . . . . . . . . . . .. . . . . . . . 7 1.3.2作为数据成员的mutex 不能保护析构.. . . . . . . . . . . . . . 8 1.4线程安全的Observer 有多难.. . . . . . . . . . . . . . . . . . . . . . . . 8 1.5原始指针有何不妥. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 11 1.6神器shared_ptr/weak_ptr . . . . . . . . . .. . . . . . . . . . . . . . . . 13 1.7插曲:系统地避免各种指针错误. . . . . . . . . . . . . . . . .. . . . . . 14 1.8应用到Observer 上.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.9再论shared_ptr 的线程安全.. . . . . . . . . . . . . . . . . . . . . . . . 17 1.10shared_ptr 技术与陷阱. . . .. . . . . . . . . . . . . . . . . . . . . . . . 19 1.11对象池. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 21 1.11.1enable_shared_from_this . . . . . . . . . . . . . . . . . . . . . . 23 1.11.2弱回调. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 24 1.12替代方案. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 26 1.13心得与小结. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . 26 1.14Observer 之谬. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 第2章线程同步精要 2.1互斥器(mutex). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.1.1只使用非递归的mutex . . . . . . . . . . . . . .. . . . . . . . . . 33 2.1.2死锁. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 35 2.2条件变量(condition variable). . . . . . . . . .
哲学家就餐问题是一个经典的并发编程问题,用来展示多线程编程中的死锁和竞争条件等问题。下面是一个使用C++多线程解决哲学家就餐问题的示例代码: ```c++ #include <iostream> #include <thread> #include <mutex> using namespace std; const int kNumPhilosophers = 5; // 哲学家数量 mutex forks[kNumPhilosophers]; // 叉子锁 void philosopher(int id) { int left_fork = id; int right_fork = (id + 1) % kNumPhilosophers; // 模拟哲学家思考 cout << "Philosopher " << id << " is thinking." << endl; this_thread::sleep_for(chrono::seconds(1)); // 尝试获得叉子 cout << "Philosopher " << id << " is hungry and wants to eat." << endl; forks[left_fork].lock(); forks[right_fork].lock(); // 开始就餐 cout << "Philosopher " << id << " is eating." << endl; this_thread::sleep_for(chrono::seconds(1)); // 释放叉子 forks[right_fork].unlock(); forks[left_fork].unlock(); // 就餐结束 cout << "Philosopher " << id << " finished eating and is thinking again." << endl; } int main() { // 创建哲学家线程 thread philosophers[kNumPhilosophers]; for (int i = 0; i < kNumPhilosophers; i++) { philosophers[i] = thread(philosopher, i); } // 等待所有哲学家线程结束 for (int i = 0; i < kNumPhilosophers; i++) { philosophers[i].join(); } return 0; } ``` 在这个示例代码中,我们使用了`mutex`来实现叉子的锁机制,每个哲学家线程都会先尝试获得左边和右边的叉子,如果获得成功就开始就餐,否则就会等待。这个示例代码简单易懂,但是仍然存在死锁和竞争条件等问题,需要进一步的优化和改进。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值