ПАРАЛЛЕЛЬНОЕ ПРОГРАММИРОВАНИЕ
162
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else CDialog::OnPaint();
}
HCURSOR CCritDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//=========================================
DWORD __stdcall Thread(void* Param)
{
CListBox *List=(CListBox*)Param;
for(int i=0;i<20;i++)
{
if (mode==1) EnterCriticalSection(&Sect1);
if (mode==2) WaitForSingleObject(hMutex,INFINITE);
GlobalCounter++;
Sleep(100);
GlobalCounter--;
char S[20];
sprintf(S,"C=%i",GlobalCounter);
if (mode==1) LeaveCriticalSection(&Sect1);
if (mode==2) ReleaseMutex(hMutex);
List->AddString(S);
}
return 0;
}
void CCritDlg::OnButton1()
{ mode=0; //Unsincronized
RunThreads();
}
void CCritDlg::OnButton2()
{
mode=1; //Use critical sections
RunThreads();
}
void CCritDlg::OnButton3()
{
mode=2; //Use mutex
RunThreads();
}
void CCritDlg::RunThreads()
{
1...,154,155,156,157,158,159,160,161,162,163 165,166,167,168,169,170,171,172,173,174,...180