检查 Win32 线程是否正在运行或处于挂起状态 [英] Check if a Win32 thread is running or in a suspended state

查看:48
本文介绍了检查 Win32 线程是否正在运行或处于挂起状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查 Win32 线程是否正在运行或处于挂起状态?

我找不到任何提供线程状态的 Win32 API.那么如何获取线程状态?

解决方案

我认为 - 最初 - 没有提供此信息,因为任何提供此信息的 API 都会产生误导和无用.

考虑两种可能的情况 - 当前线程已挂起感兴趣的线程.当前线程中的代码知道挂起状态并且应该能够共享它,因此内核团队无需添加 API.

第二种情况,系统中的其他/第三个线程已挂起感兴趣的线程(并且无法跟踪是哪个线程).现在你有一个竞争条件 - 其他线程可以在任何时候 - 取消挂起感兴趣的线程,从 API 收集的信息是无用的 - 你有一个值表明线程实际上是挂起的,而不是.

这个故事的寓意——如果你想知道一个线程被挂起——挂起它: SuspendThread 的返回值是线程的前一个挂起计数.现在您确实知道了一些有用的东西 - 线程 WAS AND STILL IS 挂起 - 这很有用.或者它没有(但现在)被暂停.无论哪种方式,线程的状态现在都是确定性已知的,因此理论上您可以基于此做出一些明智的选择 - 是 ResumeThread 还是保持挂起状态.

How do I check to see if a Win32 thread is running or in suspended state?

I can't find any Win32 API which gives the state of a thread. So how do I get the thread state?

解决方案

I think - originally - this information was not provided because any API that provided this info would be misleading and useless.

Consider two possible cases - the current thread has suspended the thread-of-interest. Code in the current thread knows about the suspended state and should be able to share it so theres no need for the kernel team to add an API.

The 2nd case, some other / a 3rd thread in the system has suspended the thread of interest (and theres no way to track which thread that was). Now you have a race condition - that other thread could, at any time - unsuspend the thread of interest and the information gleaned from the API is useless - you have a value indicating the thread is suspended when it is in fact, not.

Moral of the story - if you want to know that a thread is suspended - suspend it: The return value from SuspendThread is the previous suspend count of the thread. And now you DO know something useful - The thread WAS AND STILL IS suspended - which is useful. Or that it WASN't (but now is) suspended. Either way, the thread's state is now deterministically known so you can in theory make some intelligent choices based on that - whether to ResumeThread, or keep it suspended.

这篇关于检查 Win32 线程是否正在运行或处于挂起状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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