Reputation: 172
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()
?
Upvotes: 0
Views: 504
Reputation: 10425
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.
Upvotes: 1