Thread: Waiting For A Thread To Idle

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Red face Waiting For A Thread To Idle

    Hello,

    I'm trying to create a little program that has a macro playback-style facility which may be invoked from the command line. The main part of the program runs in a separate thread to that which calls WinMain. So:-
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        DWORD dwThreadID;
    
        // application initialization
        g_hThread = CreateThread(NULL, 0, MainThread, NULL, 0, &dwThreadID);
    ...
    This separate thread begins by creating all the windows associated with the program.
    Now, as I want to be able to interact with these windows while I am playing back macro data I want to make sure that they exist. There are lots of ways of doing this, including waiting until a global variable contains something other than 0 or NULL (the other thread has finished processing and set it). However, what I would like to know is whether there is a function that will block until a given thread has become idle.

    When MainThread has finished initialization it should go into the usual message pumping loop, so I am hoping that there is a way of detecting it blocking on GetMesssage?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can use Windows events for this. Be careful of thinking a global variable or some type of flag you create will work in multi-threading b/c it probably won't.

    However you can use WaitForMultipleObjects() and WaitForSingleObject() which will block until an event is set/reset. How you do all of this depends on if you are creating manual or auto reset events. But in the end to get multithreading working and stable you will need to rely on kernel level objects managed by the OS.

    You will also need to make use of critical sections and/or mutexes where appropriate to prevent multiple threads from altering the same data at the same time causing some serious issues.

    You can look at multithreading in the SDK. It has a pretty good explanation of each function and object available to you.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I guess an event is the simplest way of doing it.

    As my macro playback mainly involves sending messages to the various windows I hope that I can avoid having to come up with the whole thread-safe doodad.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  4. Closing thread handles from within
    By Sang-drax in forum Windows Programming
    Replies: 6
    Last Post: 09-26-2003, 12:18 PM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM