为什么C Pthread自行退出而没有过程崩溃

why c++ pthread exited by itself without process crash?

本文关键字:过程 崩溃 退出 Pthread 为什么      更新时间:2023-10-16

我有一个使用近16个线程的过程。线程ID的工作时间在工作后自行退出(请注意过程没有崩溃)。我不确定如何寻找线程退出的原因?我尝试使用打印语句,但这似乎无济于事。尝试通过GDB捕获没有帮助。如果这是某种内存损坏,那么该过程应该崩溃,并且核心文件应该告诉所有内容(大多数时候),但是过程仍在运行,只是线程使我的工作变得困难。

您可以建议某种方法来调试此问题吗?

-arpit

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

我只能想到两种方法:

  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

然后您有案例1,您需要逐步浏览线程功能以找出返回的位置(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