为什么c ++ pthread自身退出而没有进程崩溃?

时间:2019-02-15 10:10:27

标签: c++ linux gdb kernel pthreads

我有一个使用将近16个线程的进程。工作一段时间后,其中一个线程ID会自行退出(请注意进程没有崩溃)。我不确定如何查找线程退出的原因?我尝试使用打印语句,但这似乎无济于事。尝试通过gdb捕获没有帮助。如果这是某种程度的内存损坏,则该进程应该崩溃,并且核心文件应该(在大多数情况下)告诉了一切,但是进程仍然在运行,仅线程退出使我的工作变得困难。

能否请您提出一些调试此问题的方法?

-Arpit

1 个答案:

答案 0 :(得分:0)

  

我不确定如何查找线程退出的原因

只有两种方法可以实现此目的:

  1. 线程函数返回
  2. 线程函数调用执行syscall(SYS_exit, ...)

您可以先使用(gdb) catch syscall exit,然后再使用where找出发生这种情况的地方。

如果where显示类似以下内容:

(gdb) where
#0  0x00007ffff7bc3556 in __exit_thread () at ../sysdeps/unix/sysv/linux/exit-thread.h:36
#1  start_thread (arg=0x7ffff781c700) at pthread_create.c:478
#2  0x00007ffff7905a8f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97

那么您有第一种情况,您需要逐步检查线程函数以找出返回的位置(https://rr-project.org/可能会很有帮助)。

相反,如果您看到类似这样的内容:

(gdb) where
#0  syscall () at ../sysdeps/unix/sysv/linux/x86_64/syscall.S:38
#1  0x0000555555554746 in do_work () at t.c:9
#2  0x0000555555554762 in fn (p=0x0) at t.c:15
#3  0x00007ffff7bc3494 in start_thread (arg=0x7ffff781c700) at pthread_create.c:333
#4  0x00007ffff7905a8f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97

那么罪魁祸首就是所谓的syscall