我如何知道线程已完成其任务?
举个例子:-
我使用 CreateThread
Api 创建了线程 pThread
,它将执行一些任务,比如 vSampleTask
我怎么知道 pThread 已经完成它的任务了吗?
谢谢
Say for an example:-
I have created thread pThread
using CreateThread
Api which will perform some task say vSampleTask
How will i know that pThread has completed its task?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
WaitForSingleObject
或其他等待函数之一。您可以使用MsgWaitForMultipleObjects
例如,允许您的等待被输入消息打断。当线程执行完成时,线程句柄将发出信号。作为替代方法,您可以通过调用
GetExitCodeThread
。如果线程仍然繁忙,则返回FALSE
;如果线程已完成,则返回TRUE
。如果线程已完成,则还将返回退出代码。如果一个线程需要等待另一个线程完成,那么您应该使用等待函数,而不是调用
GetExitCodeThread
的繁忙轮询循环。繁忙的循环和轮询只会消耗不必要的 CPU(和电量)。等待函数允许等待线程变得空闲。You can wait on the thread handle with
WaitForSingleObject
or one of the other wait functions. You can useMsgWaitForMultipleObjects
to allow your wait to be interrupted by input messages, for example. The thread handle becomes signaled when the thread's execution has completed.As an alternative, you can check on a thread's status by calling
GetExitCodeThread
. This will returnFALSE
if the thread is still busy, andTRUE
if it has completed. If the thread has completed, then the exit code will also be returned.If one thread needs to wait for another to be complete then you should use the wait functions rather than a busy polling loop calling
GetExitCodeThread
. Busy loops and polling will just consume needless amounts of CPU (and power). Wait functions allow the waiting thread to become idle.您可以使用
GetExitCodeThread
来询问线程的状态。You can get
GetExitCodeThread
to ask for the status of the thread.