windows 在 C++ 中暂停和恢复线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5720326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:41:24  来源:igfitidea点击:

Suspending and resuming threads in c++

c++windowsresumesuspend

提问by Idov


My application has to suspend and resume a different process every few *microsec*s.
It works fine only sometimes it feels like it suspends the process for non-uniforms times.
I use the win API: ResumeThreadand SuspendThread.


我的应用程序每隔几微秒就必须暂停和恢复一个不同的进程。
它只是在有时感觉它会在非统一时间暂停过程时才能正常工作。
我使用 win API:ResumeThreadSuspendThread.

On the other hand, I tried something a little different.
I suspended the thread as usual with SuspendThreadbut when I resume it, I do like so:

另一方面,我尝试了一些不同的东西。
我像往常一样暂停了线程,SuspendThread但是当我恢复它时,我喜欢这样:

while (ResumeThread(threadHandle) > 0);

and it works faster and it runs the other process in a uniform pace.
Why does it happen? is it possible that sometimes the thread gets suspended twice and then the ResumeThread command executes?
thanks :)

它运行得更快,并且以统一的速度运行另一个进程。
为什么会发生?是否有可能有时线程被挂起两次然后 ResumeThread 命令执行?
谢谢 :)

回答by Eddy

SuspendThread()call does not suspend a thread instantly. It takes several time to save an execution context, so that ResumeThread()might be called when a thread has not suspended yet. That's why while (ResumeThread(threadHandle) > 0);works. To determine the current thread state you can call NtQueryInformationThread(), but only in NT versions of Windows.

SuspendThread()call 不会立即挂起线程。保存执行上下文需要一些时间,因此ResumeThread()可能会在线程尚未挂起时调用。这就是为什么while (ResumeThread(threadHandle) > 0);有效。要确定当前线程状态,您可以调用NtQueryInformationThread(),但仅限于 NT 版本的 Windows。

If you have a loop in the secondary thread, you can change your synchronization with a Manual Reset Event. The primary thread should call ResetEvent()to suspend a thread, and SetEvent()to resume. The secondary thread every loop should call WaitForSingleObjectEx().

如果辅助线程中有循环,则可以使用手动重置事件更改同步。主线程应该调用ResetEvent()挂起线程,然后SetEvent()恢复。每个循环都应调用的辅助线程WaitForSingleObjectEx()

回答by Ajay Sanghi

I followed Damon's suggestion, removed suspend / resume from the code and instead used a synchronization object over which my pool thread waits infinitely after completing the work and the same is signaled by my server thread after allocating work to it.

我遵循了达蒙的建议,从代码中删除了挂起/恢复,而是使用了一个同步对象,我的池线程在完成工作后无限等待,并且在向它分配工作后,我的服务器线程会发出同样的信号。

The only time I have to use suspend now is when I create the thread for the first time and after allocating work to it the thread is resumed from my server thread. The thread created is used in a thread pool over and over again to do some work.

我现在必须使用挂起的唯一时间是当我第一次创建线程并且在为它分配工作之后线程从我的服务器线程恢复。创建的线程在线程池中反复使用以做一些工作。

It is working like a charm.

它就像一个魅力。

Thank you Damon!

谢谢达蒙!

Regards,

问候,

Ajay

阿杰

回答by dbg

thats the way de loop look's like

这就是 de loop 的样子

for(i=0;i<num;i++) {     
  while (ResumeThread(threadHandle) > 0);
  ResumeThread(threadHandle)
  SuspendThread(threadHandle);      
}

SuspendThreadtakes a few milliseconds so the while loop goes on until thread is suspended, after that, again the thread process SuspendThreadfunction is called, a good way to call GetProcessContextto see EIP

SuspendThread需要几毫秒,所以while循环一直持续到线程被挂起,之后,再次SuspendThread调用线程进程函数,调用GetProcessContext查看EIP的好方法