使用gdb调试多线程死锁

gdb,神器也,必须好好深入学习之

编写一段会造成死锁的代码,如下:

#include <unistd.h>
#include <stdio.h>
#include <boost/thread.hpp>

boost::mutex mu1;
boost::mutex mu2;

void func1() {
    mu1.lock();
    sleep(1);
    mu2.lock();
    printf("func1\n");
    mu2.unlock();
    mu1.unlock();
}

void func2() {
    mu2.lock();
    sleep(1);
    mu1.lock();
    printf("func2\n");
    mu1.unlock();
    mu2.unlock();
}

int main() {
    boost::thread t1(func1);
    boost::thread t2(func2);
    t1.join();
    t2.join();
    printf("Hello, world\n");

    return 0;
}

构建程序,生成可执行文件test

g++ -g -o test test.cpp -lboost_thread

这里,使用 -g 选项,在生成的可执行文件中加入调试信息,程序中试用了boost的线程库,所以要链接boost_thread

执行程序,./test,无任何输出,死锁了

另开一个终端,执行 ps -ef | grep test,查找进程test的pid,输出如下:

</pre><pre>

simic@ubuntu:~$ ps -ef | grep test
simic     2566  2480  0 19:59 pts/1    00:00:00 ./test
simic     2653  2326  0 20:02 pts/0    00:00:00 grep --color=auto test
simic@ubuntu:~$

可知test进程的pid为2566

打开gdb

simic@ubuntu:~$ gdb
GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb)

附着到test进程上</

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值