Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Strange Thread Problem In VC++

This topic is 4415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I created a thread w/ THREAD_PRIORITY_TIME_CRITICAL. I also created an event using m_hStop = CreateEvent(NULL, TRUE, FALSE, NULL); I've noticed my thread does not always catch my event when I call SetEvent. Is this possible or should I look elsewhere for my problem? Thanks. I wait for my thread to exit like this: SetEvent(m_hStop); //wait for thread to exit while( (ExitCode == STILL_ACTIVE) && (NoError == TRUE) ) { Sleep(5); NoError = GetExitCodeThread(m_hMyThread, &ExitCode); } MyThread() { .... ..... while(bDone == false) { EventState = WaitForSingleObject(m_hStop, 0); switch(EventState) { case WAIT_TIMEOUT: //Do stuff .... break; case WAIT_OBJECT_0: bDone = true; //stop thread } } ExitThread(Stat); return(Stat); }

Share this post


Link to post
Share on other sites
Advertisement
Try setting the event and then waiting on the thread handle instead, e.g.

HANDLE hThread = CreateThread(...);

// Do stuff...

SetEvent(m_hStop);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread); // Always remember to close the thread handle!

Share this post


Link to post
Share on other sites
Are you sure the event is being set? If the thread running MyThread() is at time-critical (i.e., very high) priority, and never yeilds the CPU, your other thread may not get chance to run and set the event.

Share this post


Link to post
Share on other sites
Messing with thread priority often results in situations like this. Try a more friendly setting for your thread. In fact, it's more often the case that you'd want to set up LOWER priority threads than the parent, for housekeeping tasks, and leave the main thread with its default priority.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!