Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
CWinThread *m_eventHandler;
CEvent m_cEvent;
bool m_bShutdown = false;

Main thread:

m_bShutdown = true;
m_cEvent.SetEvent();
if(WAIT_TIMEOUT == WaitForSingleObject(m_eventHandler->m_hThread, 30000))
{
// error
}

Worker thread:

while(!m_bShutdown)
{
WaitForSingleObject(m_cEvent, INFINITE);
// do work
}
return 0;
Posted
Comments
Sergey Alexandrovich Kryukov 30-Jun-15 11:28am    
The question makes no sense, because we don't know what are you waiting for. If there is something which may or may not complete and signal the objects you wait for 30 sec, you simply create a race condition. What you consider an "error" (first comment in your code) might be not error. It smells a technology abuse, but we cannot see what you are really doing and why.
—SA
Member 10624320 30-Jun-15 12:42pm    
Thanks Sergey for the reply. I'm sorry I didn't put the question properly. My worker thread is supposed to be handling certain asynchronous events (//do work part). In my present case, I know for sure that no events aren't coming. In my main thread, I'm setting m_bShutdown to true and signaling the m_cEvent to notify the worker thread to exit. Normally, the thread ends gracefully and my WaitForSingleObject returns immediately. But some times, I get a WAIT_TIMEOUT even though I know for sure that my worker thread isn't processing any events.

In my main thread, I'm waiting for the worker thread (m_EventHandler) to exit gracefully. It happens most of the time, but of late it's failing intermittently. I read somewhere that the worker thread can terminate too quickly causing this to happen. But I've set m_bAutoDelete to FALSE, so even if the thread function terminates, I'd be waiting on a valid handle in the main thread.
Sergey Alexandrovich Kryukov 30-Jun-15 14:18pm    
What to do depends on the purpose. Also, you don't have to exit gracefully. First of all, terminology: "main thread" could be a UI thread or not (this is the thread where Main is called; strictly speaking, not always, but exclusions are extremely rare). Other threads are usually non-UI threads (exclusions are very rare). I guess by "working" you mean non-UI thread. So, very typically, I create a permanent non-UI thread and feed it tasks, never terminating it. In this case, you can simply abort it at the end of the application process, and even abort can be safe. This is just one of the possible designs, but it's apparent that creating and terminating threads over and over is usually not the best solutions.

The essence of your problem is waiting for so long time, and perhaps waiting at all. Too strong synchronization between threads simply kill parallelism. Some artists here already managed to create several threads waiting for each other so only one thread at a time worked. I'm not saying you are doing such a stupid thing, I just invite you to think about it. I cannot advise anything more certain; to give such advice, I would need to understand a lot more about your goals, especially ultimate goals.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900