Abhineet
Abhineet

Reputation: 6627

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

Upvotes: 0

Views: 257

Answers (2)

David Heffernan
David Heffernan

Reputation: 613003

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.

Upvotes: 4

A.H.
A.H.

Reputation: 66263

You can get GetExitCodeThread to ask for the status of the thread.

Upvotes: 3

Related Questions