Hi,
I want to know that how to identify that whether thread is running or not in C++ (on windows and linux both).
Thanks
Navdeep
Printable View
Hi,
I want to know that how to identify that whether thread is running or not in C++ (on windows and linux both).
Thanks
Navdeep
On Windows, use GetExitCodeThread and check return value STILL_ACTIVE.
Off the top of my head (read: without guarantee) you can do the following in Linux:
- pthreads
You can use the function 'pthread_kill' and pass 0 as the signal. If 0 is returned the thread is still running - if not equal to 0 the thread has already terminated.
- NTPL
You use the function 'kill' instead, passing 0 as the signal. Return values as specified above.
Linux OS assign the same ID to another thread/process, and pthread_kill return value may be incorrect.Quote:
You can use the function 'pthread_kill' and pass 0 as the signal. If 0 is returned the thread is still running - if not equal to 0 the thread has already terminated.
You need to use pthread_key_create, pthread_setspecific and related functions to register a "thread destructor" to get the same functionality.
Hi,
In windows you can use waitforsingleobject() on thread handle to determine thread handle state is signeled or nonsigneled, i.e terminated or alive.
-Anant
You can! But if the thread is alive, this call would be blocking call. If you need just the "state" of thread, you need to use GetExitCodeThread function only, which would return instantly irrespective of thread's actual state.Quote:
n windows you can use waitforsingleobject() on thread handle to determine thread handle state is signeled or nonsigneled, i.e terminated or alive.
hi,
yes it is true...it is blocking call. it is useful in case where,
we want to do something like cleanup on termination of thread, as waitforsingleobject won't consume any cpu cycles, it is useful instead of calling GetExitCodeThread() in iteration.
for knowing status only GetExitCodeThread() is the right option.
-Anant
WaitForSingleObject, used with a 0 timeout (non-blocking call), might still be the best option even only for checking the status of the thread. Just make sure that the thread was created using CreateThread or _beginthreadex, and not beginthread. If you want to use GetExitCodeThread, make sure that the thread does not return the value 259 as an error code.
http://msdn.microsoft.com/en-us/library/ms683190.aspx