|
-
December 16th, 2008, 03:53 AM
#1
Is Thread Alive or Not
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
-
December 16th, 2008, 06:12 AM
#2
Re: Is Thread Alive or Not
On Windows, use GetExitCodeThread and check return value STILL_ACTIVE.
Nobody cares how it works as long as it works
-
December 27th, 2008, 05:19 PM
#3
Re: Is Thread Alive or Not
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.
-
December 29th, 2008, 05:33 AM
#4
Re: Is Thread Alive or Not
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.
Linux OS assign the same ID to another thread/process, and pthread_kill return value may be incorrect.
You need to use pthread_key_create, pthread_setspecific and related functions to register a "thread destructor" to get the same functionality.
-
January 2nd, 2009, 01:02 AM
#5
Re: Is Thread Alive or Not
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
"Devise the simplest possible solution that solves the problems"
-
January 2nd, 2009, 01:12 AM
#6
Re: Is Thread Alive or Not
n windows you can use waitforsingleobject() on thread handle to determine thread handle state is signeled or nonsigneled, i.e terminated or alive.
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.
-
January 2nd, 2009, 03:53 AM
#7
Re: Is Thread Alive or Not
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
"Devise the simplest possible solution that solves the problems"
-
January 2nd, 2009, 08:07 AM
#8
Re: Is Thread Alive or Not
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|