请问Win32的ResumeThread()函数,是不是只有当返回值为0时,才真正被唤醒?

yrisxfyh 2015-01-03 04:11:00
很奇怪我只挂起了一次(SuspendThread()函数只调用了一次),可以当我调用ResumeThread()进行唤醒时,返回值却是2
于是我循环调用ResumeThread()函数,直到返回值为0,才break,这样,才真正被唤醒,

是这样的么????
...全文
162 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Evankaka 版主 2015-01-04
  • 打赏
  • 举报
回复
自己百度下吧,
赵4老师 2015-01-04
  • 打赏
  • 举报
回复
ResumeThread The ResumeThread function decrements a thread's suspend count. When the suspend count is decremented to zero, the execution of the thread is resumed. DWORD ResumeThread( HANDLE hThread // identifies thread to restart ); Parameters hThread Specifies a handle for the thread to be restarted. Windows NT: The handle must have THREAD_SUSPEND_RESUME access to the thread. Return Values If the function succeeds, the return value is the thread's previous suspend count. If the function fails, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError. Remarks The ResumeThread function checks the suspend count of the subject thread. If the suspend count is 0, the thread is not currently suspended. Otherwise, the subject thread's suspend count is decremented. If the resulting value is 0, then the execution of the subject thread is resumed. If the return value is 0, the specified thread was not suspended. If the return value is 1, the specified thread was suspended but was restarted. If the return value is greater than 1, the specified thread is still suspended. Note that while reporting debug events, all threads within the reporting process are frozen. Debuggers are expected to use the SuspendThread and ResumeThread functions to limit the set of threads that can execute within a process. By suspending all threads in a process except for the one reporting a debug event, it is possible to "single step" a single thread. The other threads are not released by a continue operation if they are suspended. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also Processes and Threads Overview, Process and Thread Functions, SuspendThread
图灵狗 2015-01-04
  • 打赏
  • 举报
回复
ResumeThread函数并不能保证线程真地继续执行,为什么呢?每一个线程都有一个线程暂停计数器,当线程正在运行时计数器为0,当其他线程对此线程使用SuspendThread函数时计数器会增加1,调用ResumeThread会使计数器减小1,所以当调用SuspendThread函数后计数器变为1。但是Windows是一个多线程操作系统,所以很有可能某个其他的线程也对此线程调用了SuspendThread,这时计数器就会变为2,这时再调用ResumeThread只会使计数器变回为1,线程将继续暂停,直到计数器变为0。那么,如何确定线程是否真地被继续执行了呢?很简单,检查函数返回值就可以了。如果返回值为0,则表示线程已经恢复执行了,如果不为0,则表示线程继续被暂停,如果为0xffffffff,则说明函数调用失败了。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧