Home > Software engineering >  Doubts about the thread deadlock
Doubts about the thread deadlock

Time:10-09

routine 1
Struct threadinfo
{
UINT nMillisecod;
CProgressCtrl * pctrlProgress;
};
UINT ThreadFunc (LPVOID lpParam)
{
LpParam threadinfo * pInfo=(threadinfo *);
for (int i=0; I & lt; 100; I++)
{
Int nTemp=pInfo - & gt; NMillisecod;
PInfo - & gt; PctrlProgress - & gt; SetPos (I);
Sleep (nTemp);
}
return 0;
}
Threadinfo Info;
Void Cmultithread3Dlg: : OnBnClickedStart ()
{
The UpdateData (TRUE);
Info. NMillisecod=m_mMillisecond;
Info. PctrlProgress=& amp; M_Ctrlprogress;
HThread=CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) ThreadFunc, & amp; Info, 0, & amp; ThreadID);
GetDlgItem (IDC_START) - & gt; The EnableWindow (FALSE);
//WaitForSingleObject (hThread, INFINITE);
GetDlgItem (IDC_START) - & gt; The EnableWindow (TRUE);
}
In this if you don't put the WaitForSingleObject (hThread, INFINITE); Commented out, there will be a deadlock, but in another example of buzzer will not deadlock, I don't feel these two examples is not the same, why would progress bar control the deadlock?
attached buzzer routines
Void ThreadFunc (int integer)
{
int i;
for(i=0; I{
Beep (500500);
Sleep (1000);
}
}
Void CMultiThread2Dlg: : OnStart ()
{
The UpdateData (TRUE);
Int integer=m_nCount;
HThread=CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) ThreadFunc, (VOID *) integer, 0, & amp; ThreadID);
GetDlgItem (IDC_START) - & gt; The EnableWindow (FALSE);
The WaitForSingleObject (hThread, INFINITE);
GetDlgItem (IDC_START) - & gt; The EnableWindow (TRUE);
}

CodePudding user response:

You wait here will rent the UI thread, the UI thread is not waiting for, will cause the interface feign death

CodePudding user response:

The cause of the deadlock is two threads waiting for each other, including implicit wait

The previous example pctrlProgress - & gt; SetPos (I);
We use SendMessage SetPos, this contains implicit wait, two hung with each other, etc are a must

CodePudding user response:

Ok, I see thanks to solve!
  • Related