在TID状态更改时获取信号 [英] Get signal when tid status change

查看:88
本文介绍了在TID状态更改时获取信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以通过 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆