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

data->hThreads[ID_THREAD_Y] = CreateThread(0,0,ThreadYValue,data,0,0);

data->hThreads[ID_THREAD_GAMMA] = CreateThread(0,0,ThreadCoefGamma,data,0,0);

data->hThreads[ID_THREAD_INTEGRAND] = CreateThread(0,0,ThreadIntegrand,data,0,0);

data->hThreads[ID_THREAD_FILE]= CreateThread(0,0,ThreadOutputToFile,data,0,0);

while (true)

{

WaitForSingleObject(data->hEventMain, INFINITE);

if(WaitForSingleObject(hEventExit, 1) == WAIT_OBJECT_0) // ïðèëîæåíèå çàâåðøàåòñÿ

{

//ïðîáóäèòü âñå ïîòîêè äëÿ âûõîäà:

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

SetEvent(data->hEvents[i]);

break;

}

data->time = clock(); // çàñå÷ü âðåìÿ âûïîëíåíèÿ

// Çäåñü íåïîñðåäñòâåííî èä¸ò âû÷èñëåíèå èíòåãðàëà!

data->result = 0;

for (int i = 0; i < data->numOfParts; i++)

{

data->xCurrent = data->xBegin + i * data->deltaX;

SetEvent(data->hEvents[ID_EVENT_ALPHA]);

WaitForSingleObject(data->hEvents[ID_EVENT_INTEGRAL], INFINITE);

data->result += data->func * data->deltaX;

ResetEvent(data->hEvents[ID_EVENT_INTEGRAL]);

}

data->time = clock() - data->time;          // çàôèêñèðîâàòü âðåìÿ âûïîëíåíèÿ

data->time = data->time / CLOCKS_PER_SEC;

sprintf(data->messageLog, "           Èíòåðâàë %s\nËåâàÿ ãðàíèöà:    %f\nÏðàâàÿ ãðàíèöà:   %f\n×èñëî ðàçáèåíèé:  %d\nÐåçóëüòàò:        %f\nÂðåìÿ âûïîëíåíèÿ: %f",

data->intervalName, data->xBegin, data->xEnd, data->numOfParts, data->result, data->time);

sprintf(data->message, "              Èíòåðâàë %s\nËåâàÿ ãðàíèöà:          %f\nÏðàâàÿ ãðàíèöà:       %f\n×èñëî ðàçáèåíèé:    %d\nÐåçóëüòàò:                   %f\nÂðåìÿ âûïîëíåíèÿ: %f",

data->intervalName, data->xBegin, data->xEnd, data->numOfParts, data->result, data->time);

SetEvent(data->hEvents[ID_EVENT_FILE]); // äàòü ñèãíàë íà çàïèñü â ôàéë

WaitForSingleObject(data->hEvents[ID_EVENT_INTEGRAL], INFINITE); // æäàòü, ïîêóäà çàïèøèòñÿ

ResetEvent(data->hEvents[ID_EVENT_INTEGRAL]);

SetEvent(data->hEventReady); // äàòü ñèãíàë íà âûâîä ñîîáùåíèÿ

WaitForSingleObject(data->hEvents[ID_EVENT_INTEGRAL], INFINITE); // æäàòü, ïîêóäà âûâåäåòñÿ

ResetEvent(data->hEvents[ID_EVENT_INTEGRAL]);

ResetEvent(data->hEventMain);

}

WaitForMultipleObjects(NUM_THREADS,data->hThreads,TRUE,INFINITE);

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

CloseHandle(data->hThreads[i]);

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

CloseHandle(data->hEvents[i]);

return 0;

}

DWORD WINAPI ThreadCoefAlpha(LPVOID lParam)

{

Buffer *data = (Buffer *)lParam;

while(true)

{

WaitForSingleObject(data->hEvents[ID_EVENT_ALPHA], INFINITE); // æäàòü ñèãíàëèçàöèè ñîáûòèÿ

if(WaitForSingleObject(hEventExit, 1) == WAIT_OBJECT_0) break; //äëÿ áåçîïàñíîãî çàâåðøåíèÿ

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

{

data->alpha[i] = sin(data->xCurrent) * i;

}

ResetEvent(data->hEvents[ID_EVENT_ALPHA]); // ñáðîñèòü ñîáûòèå

SetEvent(data->hEvents[ID_EVENT_BETA]); // ñèãíàëèçèðîâàòü ïîòîê äëÿ âû÷èñëåíèÿ Áåòà

}

return 0;

}

DWORD WINAPI ThreadCoefBeta(LPVOID lParam)

{

Buffer *data = (Buffer *)lParam;

while(true)

{

WaitForSingleObject(data->hEvents[ID_EVENT_BETA], INFINITE);

if(WaitForSingleObject(hEventExit, 1) == WAIT_OBJECT_0) break; //äëÿ áåçîïàñíîãî çàâåðøåíèÿ

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

{

data->beta[i] = data->alpha[i] * cos(data->xCurrent) * i;

}

ResetEvent(data->hEvents[ID_EVENT_BETA]);

SetEvent(data->hEvents[ID_EVENT_Y]);

}

return 0;

}