CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Posts
    292

    Check for running HANDLE

    As the topic say's, I'm trying to find out how to test if a thread HANDLE is already in use. Or more simply, if a thread is already running. What if condition would I need to test to check if a thread is already in use? And how would I prevent a new thread from starting if it already exists? Thanx.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Check for running HANDLE

    Thread is running:
    Code:
    WAIT_TIMEOUT == WaitForSingleObject(hThread, 0);
    And I hardly can understand you question about preventing from starting. Could you please rephrase.
    Best regards,
    Igor

  3. #3
    Join Date
    Mar 2011
    Posts
    46

    Re: Check for running HANDLE

    The easiest way for that you would use the good old static BOOL yourself in your code


    static BOOL ThreadLaunched = FALSE;

    // When you launch the thread
    ThreadLaunched = TRUE;


    // In the thread code just before it terminates and returns (0)
    ThreadLaunched = FALSE;

    ThreadLaunched hence tells you if the thread is running.

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Check for running HANDLE

    >> The easiest way ...
    That's the incorrect way since there's no synchronization. More than one thread can not access the same memory location, where at least one thread is writing, without using synchronization primitives provided by your threading library.

    A thread handle will be in a signaled state when it is not longer running. So if it isn't signaled, then it's running (condition in post #2).

    gg

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Check for running HANDLE

    Quote Originally Posted by dellthinker View Post
    As the topic say's, I'm trying to find out how to test if a thread HANDLE is already in use. Or more simply, if a thread is already running. What if condition would I need to test to check if a thread is already in use? And how would I prevent a new thread from starting if it already exists? Thanx.
    Use the GetExitCodeThread api. In fact, while you are in msdn looking at this function, check out all the related thread api's. Read through them and understand what they do because even if you don't need them today, you're likely to remember something about them and you can find them in msdn in the future.

    For the second question, it's all about having good programming practices - which means initializing all variables, cleaning up the variables when necessary and setting the variables back to null.

    In multithreaded programming there are a few things to keep track of mainly handles for threads, events, mutexes, or critical section objects. All of these need to be initialized and cleaned up after use.

    A great way to do this is to use classes and initialize the entities to NULL in the constructor and free them in the destructor.

    Say your class has Start and Stop methods. If you initialize the thread handle to NULL in the constructor, it's a simple matter of checking it for NULL in the Start method before starting a thread.

    Threading can be extremely easy once the programmer follows a few best practices. Some programmers seem to go out of their way to make it hard.

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