线程销毁时的通知 [英] Notification when a thread is destroyed

查看:33
本文介绍了线程销毁时的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在您的应用程序中获得线程不再运行(已返回)的通知?
我知道这在内核模式下是可能的(使用 PsSetCreateThreadNotifyRoutine),但是有没有办法从用户模式知道这一点,只使用 Win32 API?

Is there a way to get a notification that a thread no longer runs (has returned) in your application?
I know this is possible in kernel mode (using PsSetCreateThreadNotifyRoutine), but is there a way to know this from user mode, using only Win32 API ?

问题是我无法控制线程中的代码,因为我的模块是库的一部分.制作一个驱动程序来监视系统不会太难,但即使是使用我的库的基本应用程序,用户也安装驱动程序很烦人.

The problem is that I can't control the code in the thread, because my module is part of a library. Making a driver to monitor the system would not be too hard, but it's annoying for the users to install a driver even for a basic application that uses my library.

我的代码使用 TLS 存储,在 Linux/Unix 下 pthread_key_create 可以带一个指向线程销毁时调用的函数的指针.但是 TlsAlloc (Windows) 没有这样的......

My code uses TLS storage, and under Linux/Unix pthread_key_create can take a pointer to a function that is called when the thread is destroyed. But TlsAlloc (Windows) has nothing like this...

提前致谢!

推荐答案

取决于你拥有什么样的图书馆.因为 DLL 可以处理 DllMain (DLL_THREAD_DETACH) 中的线程终止.MSDN 指出这是最好的处理 TLS 资源的地方.

Depends on what kind of libraray you have. For a DLL could handle the thread termination in your DllMain (DLL_THREAD_DETACH). The MSDN states that this is the best place to deal with TLS Resources.

请记住,此回调仅在线程干净退出时调用(而不是通过例如 TerminateThread())调用.

Keep in mind that this callback is only calld for a thread exiting cleanly (not by e.g TerminateThread()).

这篇关于线程销毁时的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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