Программирование синхронизации процессов в ОС Windows. Разработка программы, осуществляющей решение поставленной задачи с помощью многопоточного приложения, страница 3

CreateWindow("edit", 0, WS_BORDER | WS_CHILD | WS_VISIBLE | 0 | 0 ,140,85,80,15,hwnd, (HMENU)ID_TEXTBOX_PARTS_1,0,0);

CreateWindow("static", "Ãðàíèöû II-ãî èíòåðâàëà [C;D]", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 165, 110, 165, 15, hwnd, 0,0, 0);

CreateWindow("static", "Ëåâàÿ ãðàíèöà C:", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 10, 135, 140, 15, hwnd, 0,0, 0);

CreateWindow("static", "Ïðàâàÿ ãðàíèöà D:", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 10, 165, 140, 15, hwnd, 0,0, 0);

CreateWindow("static", "×èñëî ðàçáèåíèé:", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 10, 190, 140, 15, hwnd, 0,0, 0);

CreateWindow("edit", 0, WS_BORDER | WS_CHILD | WS_VISIBLE | 0 | 0 ,140,135,80,15,hwnd, (HMENU)ID_TEXTBOX_C,0,0);

CreateWindow("edit", 0, WS_BORDER | WS_CHILD | WS_VISIBLE | 0 | 0 ,140,165,80,15,hwnd, (HMENU)ID_TEXTBOX_D,0,0);

CreateWindow("edit", 0, WS_BORDER | WS_CHILD | WS_VISIBLE | 0 | 0 ,140,190,80,15,hwnd, (HMENU)ID_TEXTBOX_PARTS_2,0,0);

CreateWindow("button","Ïîñ÷èòàòü",WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE,165,215,165,20,hwnd,(HMENU)1,0,0);

return 0;

}

// Îáðàáàòûâàåò íàæàòèå êíîïêè.

long WM_COMMAND_ON(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

if((HIWORD(wParam) == 0) && (LOWORD(wParam) == 1))

{

bool a = WaitForSingleObject(bufAB->hEventMain, 1) == WAIT_TIMEOUT;

bool b = WaitForSingleObject(bufCD->hEventMain, 1) == WAIT_TIMEOUT;

if(a && b) // Åñëè îáà ñîáûòèÿ íàõîäÿòñÿ â íåñèãíàëüíîì ñîñòîÿíèè (ò.å. ïîòîêè äëÿ âû÷èñëåíèÿ ñâîáîäíû), òî ìîæíî ïîëó÷àòü äàííûå èç ôîðìû è ïîñûëàòü èõ íà âû÷èñëåíèå.

{

char buf[MAX_SIZE_BUF];

GetDlgItemText(hwnd,ID_TEXTBOX_A,buf,MAX_SIZE_BUF);

bufAB->xBegin = atof(buf);

GetDlgItemText(hwnd,ID_TEXTBOX_B,buf,MAX_SIZE_BUF);

bufAB->xEnd = atof(buf);

GetDlgItemText(hwnd,ID_TEXTBOX_C,buf,MAX_SIZE_BUF);

bufCD->xBegin = atof(buf);

GetDlgItemText(hwnd,ID_TEXTBOX_D,buf,MAX_SIZE_BUF);

bufCD->xEnd = atof(buf);

GetDlgItemText(hwnd,ID_TEXTBOX_PARTS_1,buf,MAX_SIZE_BUF);

bufAB->numOfParts = atoi(buf);

GetDlgItemText(hwnd,ID_TEXTBOX_PARTS_2,buf,MAX_SIZE_BUF);

bufCD->numOfParts = atoi(buf);

bufAB->deltaX = (bufAB->xEnd - bufAB->xBegin) / bufAB->numOfParts;

bufCD->deltaX = (bufCD->xEnd - bufCD->xBegin) / bufCD->numOfParts;

// Ïåðåâåñòè ñîáûòèÿ â ñèãíàëüíûå ñîñòîÿíèå, ÷òîáû íà÷àëîñü âû÷èñëåíèå.

SetEvent(bufAB->hEventMain);

SetEvent(bufCD->hEventMain);

}

else // Åñëè õîòÿ áû îäíî ñîáûòèå â ñèãíàëüíîì ñîñòîÿíèè (çàíÿòî), çíà÷èò ïîòîêè äëÿ âû÷èñëåíèÿ åù¸ çàíÿòû.

{

MessageBox(*hwndMain, " äàííûé ìîìåíò èíòåãðàë óæå âû÷èñëÿåòñÿ.\nÇàêðîéòå ýòî ñîîáùåíèå è äîæäèòåñü ðåçóëüòàòà.","Ñîîáùåíèå", MB_OK|MB_ICONWARNING);

}

}

return 0;

}

// Ïðè çàêðûòèè ïðîãðàììû.

long WM_DESTROY_ON(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

SetEvent(hEventExit); // ñèãíàëèçèðóåì î íåîáõîäèìîñòè çàâåðøåíèÿ ïðîãðàììû

// Çàïóñòèòü âñå ïîòîêè:

SetEvent(bufAB->hEventMain);

SetEvent(bufCD->hEventMain);

SetEvent(bufAB->hEventReady);

SetEvent(bufCD->hEventReady);

}

DWORD WINAPI ThreadCalcIntegral(LPVOID lParam)

{

Buffer *data = (Buffer *)lParam;

// Îïðåäåëåíèå ñîáûòèé:

for(int i = 0; i < NUM_EVENTS; i++)

data->hEvents[i] = CreateEvent(0,TRUE, FALSE, 0);

// Ñîçäàíèå ïîòîêîâ:

data->hThreads[ID_THREAD_ALPHA] = CreateThread(0,0,ThreadCoefAlpha,data,0,0);

data->hThreads[ID_THREAD_BETA] = CreateThread(0,0,ThreadCoefBeta,data,0,0);