Make a thread wait?

Started by
3 comments, last by MetaKnight 17 years, 10 months ago
I am stopping two threads like so for each thread:

if(myState == NORMAL)
{
        InterlockedIncrement(&myState);
	//myState = STOPPED;
}
while(myState == STOPPED);

now this is for two threads, now when a third thread find thats both have stopped it starts them again like so:

if(Gary->myState == STOPPED && Zoey->myState == STOPPED )
{
	Gary->myState = NORMAL;
	Zoey->myState = NORMAL;
} 

The problem is that the while loop is stupid. But I dont know another way to do this. I could try sleep but too high would waste time, and too low would be the same as a while();. So what is the best way to make a thread "wait", but not doing any instructions so that the other thread can go faster.
Advertisement
Busy Waiting.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You should use mutexes for this kind of stuff.
Functions of interest:
CreateMutex
ReleaseMutex
WaitForSingleObject
WaitForMultipleObjects
Event Objects were designed specifically to allow you to synchronize however you want. You can use the wait functions that 'Paulius Maruska' linked to to query (timeout = 0) or wait for an event to be set.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Ahh there we go.
I also tried SuspendThread, but that caused more problems then it solved.
Thanks guys!

This topic is closed to new replies.

Advertisement