CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Posts
    10

    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

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    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

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up 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"

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up 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"

  8. #8
    Join Date
    Jul 2007
    Posts
    31

    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
  •  





Click Here to Expand Forum to Full Width

Featured