Thread: How to check if a thread is running or has come out ?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    9

    How to check if a thread is running or has come out ?

    I have created a thread as follows,

    DWORD Id;
    CreateThread(0, 0, think, pcon, 0, &Id);

    How to check if the thread is running or if it has come out?
    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I have the same question, but temporarily I have mine set up like this:

    Code:
    bool thread=false;
    
    // just using your example
    DWORD WINAPI think(LPVOID pcon) {
         thread = true;
         // do stuff here
         thread = false;
         return whatever;
    }
    
    whatever whatever(whatever) {
         DWORD Id;
         CreateThread(0, 0, think, pcon, 0, &Id);
    }
    And then if you want to know if the thread is running or not, just use a simple if(thread) statement.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Save the return value of the CreateThread function.
    Use this handle in the WaitForSingleObject function with timeout=0 (or not zero if you want to wait for thread to exit)
    When you know where the state of the handle is signaled or not.

    The handle is signaled on exit of the thread.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 02:18 AM
  2. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  3. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  4. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  5. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM