Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need help resolving a deadlock

Need help resolving a deadlock

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingjsontutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Budric B
    wrote on last edited by
    #1

    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.

    B 1 Reply Last reply
    0
    • B Budric B

      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.

      B Offline
      B Offline
      Budric B
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • B Budric B

        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.

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups