在TID状态更改时获取信号 [英] Get signal when tid status change
问题描述
有一种方法可以通过 waitpid
更改pid/tid状态,但这是阻止功能.
我想监视特定pid中的所有线程,并在其中一个线程更改并打印tid时获得信号.
现在,我以该进程中的线程数打开线程,并在1个tid上每执行1个 waitpid
,然后在阻塞功能完成后,我将更改的tid打印出来.
如何获得tid更改的信号,以便可以在1个线程中监视所有tid.
我不想只监视系统特定pid/tid中的所有pid.
那些小玩意儿/小玩意儿不是我过程中的孩子.
如果您使用的是POSIX线程,则可以使用 此清理"功能随后可以将用户信号之一( 如果您使用 您可以使用 SIGUSR1
或 SIGUSR2
)发送到进程,然后捕获该进程并将其视为关于线程的信号终止. sigqueue
您可以为信号处理程序添加线程ID,以便知道刚刚退出的线程. pthread_sigmask
阻止所有线程中的用户信号,以确保仅将其传递给主进程线程(或使用 解决方案
If you're using POSIX threads, then you could use pthread_cleanup_push
and pthread_cleanup_pop
to call a "cleanup" function when your thread is exiting.
This "cleanup" function could then send one of the user signals (SIGUSR1
or SIGUSR2
) to the process which then catches it and treats it as a signal about thread termination.
If you use sigqueue
you can add the thread-id for the signal handler so it knows which thread just exited.
You can use pthread_sigmask
to block the user signal in all threads, to make sure it's only delivered to the main process thread (or use pthread_sigqueue
to send to the main process thread specifically).
这篇关于在TID状态更改时获取信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!