如何判断一个线程的状态
请问C++中用什么API可以判断一个已知HANDLE的线程的状态呢? 问题点数:20、回复次数:9Top
1 楼runi(我此刻却只想亲吻你倔强的嘴)回复于 2005-12-15 09:49:07 得分 0
我想知道该线程当前是挂起还是运行状态,应该如何判断呢?Top
2 楼captainliyun(茄子)回复于 2005-12-15 10:42:41 得分 3
在线程外创建一个事件,
然后在线程的某个位置去等这个事件,等待时间为0,
要检查线程状态的时候,在外部去 SetEvent这个事件,如果线程等到这个事件.当线程等到这个事件后,就通知外面,就可以判断线程还在运行,如果外部在规定的事件内,没有等到线程的通知,可以认为线程挂起了.Top
3 楼freemme(路在脚下)回复于 2005-12-15 11:00:21 得分 3
然后在线程的某个位置去等这个事件,等待时间为0
---------------------------
虽然设置等待时间为0,但是一般情况下最小精度是10毫秒,所以这种方式还是回影响线程效率,windows API应该有函数可以获取线程状态的。
Top
4 楼captainliyun(茄子)回复于 2005-12-16 13:09:50 得分 3
楼上的,给个解决方案噻.Top
5 楼weiym(磨刀霍霍向猪羊)回复于 2005-12-16 17:39:02 得分 0
GetExitCodeThreadTop
6 楼song_09(song)回复于 2005-12-16 20:23:39 得分 3
直接用GetExitCodeThread()函数,它的返回值表明了线程的状态
Top
7 楼hpig(喜猪)回复于 2005-12-16 21:40:21 得分 3
or WaitForSingleObject~~Top
8 楼runi(我此刻却只想亲吻你倔强的嘴)回复于 2005-12-19 11:27:53 得分 0
GetExitCodeThrea好像只能判断线程是不是还活着,而不能判断具体的挂起、休眠等状态吧?
Top
9 楼njg_jh(糨糊)回复于 2005-12-19 11:35:56 得分 5
BOOL GetExitCodeThread(
HANDLE hThread, // handle to the thread
LPDWORD lpExitCode // termination status
);
If the specified thread has not terminated, the termination status returned is STILL_ACTIVE. If the thread has terminated, the termination status returned may be one of the following:
The exit value specified in the ExitThread or TerminateThread function.
The return value from the thread function.
The exit value of the thread's process.
Top




