linux gdb调试C/C++多线程死锁的定位
2021/5/15 7:26:59
本文主要是介绍linux gdb调试C/C++多线程死锁的定位,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <thread> 2 #include <iostream> 3 #include <mutex> 4 using namespace std; 5 mutex _mutex1; 6 mutex _mutex2; 7 int date1; 8 int date2; 9 int do_work_1() 10 { 11 cout << "thread_1 start" << endl; 12 lock_guard<mutex> locker1(_mutex1); 13 date1++; 14 this_thread::sleep_for(chrono::seconds(1)); 15 lock_guard<mutex> locker2(_mutex2); 16 date2++; 17 cout << "thread_1 end" << endl; 18 return 0; 19 } 20 int do_work_2() 21 { 22 cout << "thread_2 start" << endl; 23 lock_guard<mutex> locker2(_mutex2); date2++; 25 this_thread::sleep_for(chrono::seconds(1)); 26 lock_guard<mutex> locker1(_mutex1); 27 date1++; 28 cout << "thread_2 end" << endl; 29 return 0; 30 } 31 int main() 32 { 33 thread t1(do_work_1); 34 thread t2(do_work_2); 35 t1.join(); 36 t2.join(); 37 cout << "end" << endl; 38 return 0; 39 40 }
写了一个多线程死锁的程序,在linux下
(1)g++ -Wall -g -o test mutex.cpp -lpthread
(2) gdb test
(3) 执行r
产生死锁
(4) 按下ctrl + C 使程序中断
查看当前主线程的栈帧(bt)
查看栈帧2停的位置
初步判断是产生了死锁,因为join没有执行。
查看一下当前的线程,转换一下线程,查看对应的栈帧。
查看到线程2 卡在locker2上,就可以判断产生了死锁
这篇关于linux gdb调试C/C++多线程死锁的定位的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-0600-macOS和Linux安装和管理多个Python版本
- 2024-03-30[译]漫画SELinux概念
- 2024-03-29linux 移动文件
- 2024-03-28linux .so file
- 2024-03-28Linux 磁盘管理
- 2024-03-28Linux学习笔记(十三)磁盘管理(一):磁盘分区
- 2024-03-26linux 创建 文件
- 2024-03-25使用SecureCRT对Linux vim进行颜色设置
- 2024-03-202019-2020-12 20199317 《Linux内核原理与分析》 第十二周作业
- 2024-03-20Linux运维的第二周总结