C++回调函数中调用Python函数出现的死锁问题调试及解决

本文详细介绍了在C++回调函数中调用Python代码时遇到的死锁问题的调试过程,通过gdb查看线程栈信息发现Python的全局解释器锁(GIL)未被正确获取。解决方法是在调用Python函数前后正确地获取和释放GIL,以避免未定义的行为。
摘要由CSDN通过智能技术生成

一、查找死锁原因:

1、使用gdb exe指令进入gdb命令行,再输入r运行可执行文件

gdb /home/sdhm/catkin_ws/devel/lib/gpd_ros/gpd_server
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/sdhm/catkin_ws/devel/lib/gpd_ros/gpd_server...done.
(gdb) r
Starting program: /home/sdhm/catkin_ws/devel/lib/gpd_ros/gpd_server 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffd5c41700 (LWP 1837)]
[New Thread 0x7fffd5440700 (LWP 1838)]
[New Thread 0x7fffd4c3f700 (LWP 1839)]
[New Thread 0x7fffcffff700 (LWP 1844)]
[New Thread 0x7fffcc85d700 (LWP 1847)]
[New Thread 0x7fffbdfff700 (LWP 1848)]
[New Thread 0x7fffbd7fe700 (LWP 1849)]
[New Thread 0x7fffb8ffd700 (LWP 1850)]
[New Thread 0x7fffb67fc700 (LWP 1851)]
[New Thread 0x7fffb3ffb700 (LWP 1852)]
[New Thread 0x7fffb17fa700 (LWP 1853)]
[Thread 0x7fffb17fa700 (LWP 1853) exited]
[Thread 0x7fffb3ffb700 (LWP 1852) exited]
[Thread 0x7fffb67fc700 (LWP 1851) exited]
[Thread 0x7fffb8ffd700 (LWP 1850) exited]
[Thread 0x7fffbd7fe700 (LWP 1849) exited]
[Thread 0x7fffbdfff700 (LWP 1848) exited]
[Thread 0x7fffcc85d700 (LWP 1847) exited]
[New Thread 0x7fffb17fa700 (LWP 1874)]
[New Thread 0x7fffb3ffb700 (LWP 1875)]
[New Thread 0x7fffb67fc700 (LWP 1876)]
[New Thread 0x7fffb8ffd700 (LWP 1925)]
[New Thread 0x7fff006eb700 (LWP 1926)]
[New Thread 0x7ffeffeea700 (LWP 1927)]
[New Thread 0x7ffeff6e9700 (LWP 1928)]
[New Thread 0x7ffefeee8700 (LWP 1929)]
[New Thread 0x7ffefe6e7700 (LWP 1930)]
[New Thread 0x7ffefdee6700 (LWP 1931)]
[New Thread 0x7ffefd6e5700 (LWP 1933)]
[New Thread 0x7ffefcee4700 (LWP 1935)]
[New Thread 0x7ffed7fff700 (LWP 1936)]
[New Thread 0x7ffed77fe700 (LWP 1937)]
[New Thread 0x7ffed6ffd700 (LWP 1938)]
[New Thread 0x7ffed67fc700 (LWP 1939)]
[New Thread 0x7ffed5ffb700 (LWP 1940)]
[New Thread 0x7ffed57fa700 (LWP 1941)]
[New Thread 0x7ffed4ff9700 (LWP 1942)]
[New Thread 0x7ffeb767e700 (LWP 1943)]
[New Thread 0x7ffeb6e7d700 (LWP 1944)]
[New Thread 0x7ffeb667c700 (LWP 1945)]
[New Thread 0x7ffeb5e7b700 (LWP 1946)]
[New Thread 0x7ffeb567a700 (LWP 1948)]
[New Thread 0x7ffeb4e79700 (LWP 1949)]
[New Thread 0x7ffeaffff700 (LWP 1950)]
[New Thread 0x7ffeaf7fe700 (LWP 1951)]
[New Thread 0x7ffeaeffd700 (LWP 1952)]
[New Thread 0x7ffeae7fc700 (LWP 1953)]
[New Thread 0x7ffeadffb700 (LWP 2166)]
[New Thread 0x7ffead7fa700 (LWP 2167)]
[New Thread 0x7ffeacff9700 (LWP 2168)]
[New Thread 0x7ffea7fff700 (LWP 2169)]
[New Thread 0x7ffea77fe700 (LWP 2170)]
[New Thread 0x7ffea6ffd700 (LWP 2171)]
[New Thread 0x7ffea67fc700 (LWP 2172)]

2、此时程序死锁,持续运行,但不往下走,按下ctrl + c

^C
Thread 1 "gpd_server" received signal SIGINT, Interrupt.
0x00007ffff72fcc1d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
84	../sysdeps/unix/syscall-template.S: No such file or directory.

3、查看线程栈信息,info stack,这个命令只能查看当前正在运行的某个线程的栈信息 

(gdb) inf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值