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

hwnd = CreateWindowEx (

0,

szClassName,

"Ëàáîðàòîðíàÿ ðàáîòà ¹5 - Ìíîãîïîòî÷íàÿ âåðñèÿ", // èçìåíèëè èìÿ îêíà

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

500,

285,

HWND_DESKTOP,

0,

hThisInstance,

0

);

ShowWindow (hwnd, nCmdShow);

// Ñîõðàíÿåì óêàçàòåëü íà îêíî

hwndMain = &hwnd;

// Âûäåëåíèå ïàìÿòè ïîä áóôåðû äëÿ äâóõ èíòåðâàëîâ:

bufAB = new Buffer;

strcpy(bufAB->intervalName, "AB");

bufCD = new Buffer;

strcpy(bufCD->intervalName, "CD");

// Ñîçäàíèå ðó÷íûõ ñîáûòèé â íåñèãíàëüíîì ñîñòîÿíèè:

bufAB->hEventMain = CreateEvent(0,TRUE, FALSE, 0);

bufCD->hEventMain = CreateEvent(0,TRUE, FALSE, 0);

bufAB->hEventReady = CreateEvent(0,TRUE, FALSE, 0);

bufCD->hEventReady = CreateEvent(0,TRUE, FALSE, 0);

hEventExit = CreateEvent(0,TRUE, FALSE, 0);

hSemaphoreFile = CreateSemaphore(0, 1, 1, 0);

// Ñîçäàíèå ïîòîêîâ äëÿ âû÷èñëåíèÿ èíòåãðàëîâ íà îòðåçêàõ AB è CD:

HANDLE hThreadCalcAB = CreateThread(0,0,ThreadCalcIntegral,bufAB,0,0);

HANDLE hThreadCalcCD = CreateThread(0,0,ThreadCalcIntegral,bufCD,0,0);

HANDLE hThreadFollow = CreateThread(0,0,ThreadOutputCommonInfo,0,0,0);

/* Run the message loop. It will run until GetMessage() returns 0 */

while (GetMessage (&messages, 0, 0, 0))

{

/* Translate virtual-key messages into character messages */

TranslateMessage(&messages);

/* Send message to WindowProcedure */

DispatchMessage(&messages);

}

// Îæèäàíèå çàâåðøåíèÿ ïîòîêîâ âû÷èñëåíèÿ:

WaitForSingleObject(hThreadCalcAB, INFINITE);

WaitForSingleObject(hThreadCalcCD, INFINITE);

WaitForSingleObject(hThreadFollow, INFINITE);

// Îñâîáîæäåíèå ðóñóðñîâ:

CloseHandle(hThreadCalcAB);

CloseHandle(hThreadCalcCD);

CloseHandle(hThreadFollow);

CloseHandle(hEventExit);

CloseHandle(hSemaphoreFile);

CloseHandle(bufAB->hEventMain);

CloseHandle(bufCD->hEventMain);

CloseHandle(bufAB->hEventReady);

CloseHandle(bufCD->hEventReady);

delete bufAB;

delete bufCD;

return messages.wParam;

}

// Îáðàáàòûâåò ñîáûòèÿ íà ôîðìå.

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)                  /* handle the messages */

{

case WM_CREATE: // Ñîîáùåíèå î ñîçäàíèè îêíà

WM_CREATE_ON(hwnd, message, wParam, lParam);

break;

case WM_COMMAND: // Ñîîáùåíèå î íàæàòèè êíîïêè

WM_COMMAND_ON(hwnd, message, wParam, lParam);

break;

case WM_DESTROY: // Ñîîáùåíèå î çàâåðøåíèè ïðîãðàììû

WM_DESTROY_ON(hwnd, message, wParam, lParam);

PostQuitMessage (0);       /* send a WM_QUIT to the message queue */

break;

default:                      /* for messages that we don't deal with */

return DefWindowProc (hwnd, message, wParam, lParam);

}

return 0;

}

// Ñîçäàåò ýëåìåíòû íà ôîðìå.

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

{

CreateWindow("static", "Ãðàíèöû I-ãî èíòåðâàëà [A;B]", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 165, 10, 165, 15, hwnd, 0,0, 0);

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

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

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

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

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