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

This is a discussion on How to check if a thread is running or has come out ? within the Windows Programming forums, part of the Platform Specific Boards category; I have created a thread as follows, DWORD Id; CreateThread(0, 0, think, pcon, 0, &Id); How to check if the ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21