在信号处理程序中暂停线程执行

Suspending thread execution within signal handler

这是 的后续内容。

假设我使用 sigaction 设置我自己设计的信号处理程序。在此信号处理程序中,我访问有关 CPU 状态的信息(例如线程的堆栈指针)并将其复制到内存中的预定位置。我现在想从另一个线程检查此线程堆栈上的值。

有没有办法在同一信号处理程序中暂停信号处理程序中线程的执行,以便我可以从另一个线程安全地检查该线程的堆栈? (注意:我在这里定义 "safely" 的意思是“不用担心线程完成或从函数返回。)

如果我在信号处理程序之外,我可以使用 sigsupend;然而,根据 the GNU documentation. I could also try extending a signal using the method described in this question,在信号处理程序中使用它是不安全的,但我认为任何标准 *nix 信号都不会在这里帮助我。

管道上的阻塞 readasync-signal-safe,它提供了一种方便的方式来唤醒您的 "suspended" 线程。

例如,

static int the_pipe[2] = {-1, -1};  // file descriptors allocated in main via pipe()

static void
siginfo_handler(int s, siginfo_t *si, void *ctx) {
  char c;

  // do something with (ucontext_t *)ctx

  // now wait for someone else to wake us up
  read(the_pipe[0], &c, 1);

  ...
}