简体   繁体   中英

How will I know that thread has completed its task?

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

You can wait on the thread handle with WaitForSingleObject or one of the other wait functions. You can use MsgWaitForMultipleObjects 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 return FALSE if the thread is still busy, and TRUE 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来询问线程的状态。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM