Need help resolving a deadlock
-
Hi, I have 2 threads that for some reason lock up and I can't figure out why. Maybe if someone could look at some of my code they can see something obviously wrong:
UINT CChildView::CaptureThread() { bool ShutterChanged = false; int saveShutter; while(!m_bTerminateThread) { //wait if (m_bCapturePaused) { ::SetEvent(m_hCapturePausedConfirm); switch(::WaitForSingleObject(m_hCaptureEvent, 5000)) { case WAIT_OBJECT_0: break; //break out of the switch statement case WAIT_TIMEOUT: continue; //continue with the switch statement } } ...rest of the thread Note that inside this thread I use an object which implements CCriticalSection object that I lock and unlock for each function. Both the GUI thread and this thread calls functions on this object. } Furthermore I define: inline void StartCapture() { m_bCapturePaused = FALSE; ::SetEvent(m_hCaptureEvent); ::ResetEvent(m_hCapturePausedConfirm); } inline void PauseCapture() { m_bCapturePaused = TRUE; ::ResetEvent(m_hCaptureEvent); ::WaitForSingleObject(m_hCapturePausedConfirm, INFINITE); }
The problem is in the main program when the user clicks a button I want to pause the thread. The deadlock doesn't occur always. Also any runs through the debugger work, so I don't know how to solve this problem. Thanks. -
Hi, I have 2 threads that for some reason lock up and I can't figure out why. Maybe if someone could look at some of my code they can see something obviously wrong:
UINT CChildView::CaptureThread() { bool ShutterChanged = false; int saveShutter; while(!m_bTerminateThread) { //wait if (m_bCapturePaused) { ::SetEvent(m_hCapturePausedConfirm); switch(::WaitForSingleObject(m_hCaptureEvent, 5000)) { case WAIT_OBJECT_0: break; //break out of the switch statement case WAIT_TIMEOUT: continue; //continue with the switch statement } } ...rest of the thread Note that inside this thread I use an object which implements CCriticalSection object that I lock and unlock for each function. Both the GUI thread and this thread calls functions on this object. } Furthermore I define: inline void StartCapture() { m_bCapturePaused = FALSE; ::SetEvent(m_hCaptureEvent); ::ResetEvent(m_hCapturePausedConfirm); } inline void PauseCapture() { m_bCapturePaused = TRUE; ::ResetEvent(m_hCaptureEvent); ::WaitForSingleObject(m_hCapturePausedConfirm, INFINITE); }
The problem is in the main program when the user clicks a button I want to pause the thread. The deadlock doesn't occur always. Also any runs through the debugger work, so I don't know how to solve this problem. Thanks.First, I finally found how to debug multiple threads in VC++ 6. Run in debug mode, click pause, click Debug->Threads. After I did that I immediately saw the thread executing a line CComboBox::GetCurSel(). I thought I didn't have any more GUI modification within a thread but I guess I do. I think the problem is now fixed. I guess people aren't kidding when they say don't modify the GUI from a thread.
-
First, I finally found how to debug multiple threads in VC++ 6. Run in debug mode, click pause, click Debug->Threads. After I did that I immediately saw the thread executing a line CComboBox::GetCurSel(). I thought I didn't have any more GUI modification within a thread but I guess I do. I think the problem is now fixed. I guess people aren't kidding when they say don't modify the GUI from a thread.
I guess people aren't kidding when they say don't modify the GUI from a thread. Don't modify the MFC GUI from a thread that did not create the MFC object - you can modify any of them from the same thread that created them, of course.