Question : Thread deadlock between GUI callback and worker thread?

Hi,

I have a simple object which is modified with the contents of a combo box from a CFormView - that is there is a callback function in the view called by the framework when the combo box is changed and I modify my object there.

In addition to this, I have a worker thread which loops taking values from this object and outputting them elsewhere - the problem is I cannot apply changes to this object in my formview callback until the worker loop has finished each time (where it then sleeps for a period of 50ms or so before re-looping), so I used a critical section around the body of the loop as well as the start and end of the GUI callback function.  

This appears to work ok, apart from when i change the value of the combo box very quickly, repeatedly, which causes the app to go into deadlock (it is def these 2 synchronised blocks causing this).

I've tried all the various synchronisation methods described here:

http://www.codeproject.com/KB/threads/Synchronization.aspx

And all yield the same result - I cannot see why I would be getting deadlock unless it is considered a bad idea to use synchronised blocks in framework callbacks due to Windows messages or something like that?

My code is basically as follows...

(citicalSection object is global and correctly initalised in my startup method using InitializeCriticalSection(&m_CriticalSection);)


Worker thread body...
/**
 */
DWORD CMainThread::Run(LPVOID)
{
  while(true)
  {
    EnterCriticalSection(criticalSection);
   
    // Read some basic values from my object.. (there are NO blocking calls here, literally
    // just reading ints and strings).
    ...
   
    LeaveCriticalSection(criticalSection);
    Sleep(50);
  }

CFormView GUI callback body...
/**
 */
void CUserSettingsView::OnCbnSelchangeMotionbasecombo()
{
    EnterCriticalSection(criticalSection);

    // get new combo selected index  
    int newind = ((CComboBox*) GetDlgItem(IDC_MOTIONBASECOMBO))->GetCurSel();
   
    // Do some really  trivial stuff to my object according to this index (NO blocking calls, just
    //setting ints and strings).
    ....

    LeaveCriticalSection(criticalSection);
}

Appreciate the help, hope I've posted enough here to highlight the problem.
Thanks
Chris

Answer : Thread deadlock between GUI callback and worker thread?

Hmm...

Personally I would not have implemented a thread this way; I would have used members of a thread class that pause/resume the thread as and when, instead of using a shared critical section.  But that is just stylistic...

I take it you have paused your program and found out where the deadlock is occurring.  I would say that the cause is the call to GetCurSel(), which internally uses SendMessage() which is itself synchronous.

Put some tracepoints into your code (a breakpoint, except when hit it does not break execution and outputs a message), or if you don't have this functionality in your Visual Studio, add some TRACE statements.  One after the GetCurSel() call, and the other immediately after the EnterCriticalSection() within your thread's Run().  When you get deadlock, see what the last message traced was and report back.
Random Solutions  
 
programming4us programming4us