简体   繁体   中英

Multi-threading with WinApi in C++

I've been struggling with multi-threading for a while now...managed to work it out last night on my current program, what I wanted is to have a simple timer running while I'm still able to work with my application.

I used msdn reference .

I managed to make it work fine ( or at least I think it works fine ) with this code:

case ID_MENUBUTTON0:

        hRunMutex = CreateMutex(NULL, TRUE, NULL);
        _beginthread(Sistem::timeFrame, 0, NULL);
        break;

case ID_MENUBUTTON4:
        ReleaseMutex(hRunMutex);
        PostQuitMessage(0);
        break;

Sistem::timeFrame func calculates time with intervals of 100ms Sleep(100); and outputs it in a static window and updates the window every 100ms.

Now what I wanted to ask since I didn't really understand all the fuss about this is why is there so many extra stuff ? Such as WaitForSingleObject , I've seen a lot of it but can't figure out it's practical use. Is there something I should watch out for except for ReleaseMutex() ?

WaitForSingleObject and WaitForMultipleObjects are used to make a thread wait until another thread has done something. The practical use is coordination, like don't land the plane until the wheels are down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM