mySolver2Time = clock();
IntegralSolver::GetAnswer(mySolver2->myArgs);
HANDLE hnlds[2];
hnlds[0] = tChecker1;
hnlds[1] = tChecker2;
WaitForMultipleObjects(2, hnlds, true, INFINITE);
swprintf(str, MAX_SIZE,
L"Значение интеграла на отрезке [%.3f;%.3f] равно %.3f\nВремя выполнения: %.3f секунд.\n\nЗначение интеграла на отрезке [%.3f;%.3f] равно %.3f\nВремя выполнения: %.3f секунд.",
mySolver1->start, mySolver1->end, mySolver1->answer, (double)mySolver1Time / CLOCKS_PER_SEC,
mySolver2->start, mySolver2->end, mySolver2->answer, (double)mySolver2Time / CLOCKS_PER_SEC);
MessageBox(_hwnd, str, L"Ответ", MB_OK | MB_ICONEXCLAMATION);
delete mySolver1; delete mySolver2;
mySolver1 = nullptr; mySolver2 = nullptr;
CloseHandle(tChecker1); CloseHandle(tChecker2);
}
return 0;
}
unsigned int _stdcall SolverChecker(void* pParam)
{
ExtendArgs* ea = static_cast<ExtendArgs*>(pParam);
WaitForSingleObject(*(ea->args->isSolved), INFINITE);
*(ea->time) = clock() - *(ea->time);
return 0;
}
Листинг IntegralSolver:
#define _CRT_SECURE_NO_DEPRECATE
#include <math.h>
#include <Windows.h>
#include "IntegralSolver.h"
using namespace std;
void WAIT(long* param, long state)
{
while (InterlockedExchange(param, state) == state)
Sleep(0);
}
void FREE(long* param, long state)
{
InterlockedExchange(param, state);
}
IntegralSolver::IntegralSolver(double start, double end, double dx, char* filename)
{
this->start = start;
this->end = end;
this->dx = dx;
aLock = bLock = gLock = yLock = funcLock = 1;
startLock = 0;
this->SetFileName(filename);
mutex = CreateMutex(NULL, true, nullptr);
isSolved = CreateEvent(NULL, true, false, nullptr);
for (size_t i = 0; i < 1001; i++)
{
bufferAlpha[i] = 0;
bufferBeta[i] = 0;
bufferGamma[i] = 0;
bufferIgrek[i] = 0;
}
int splitCount = (end - start) / dx;
bufferFunc = new long double[splitCount];
for (size_t i = 0; i < splitCount; i++)
{
bufferFunc[i] = 0;
}
myArgs = new Args;
myArgs->file = file;
myArgs->start = this->start;
myArgs->end = this->end;
myArgs->dx = this->dx;
myArgs->answer = &answer;
myArgs->aLock = &aLock;
myArgs->bLock = &bLock;
myArgs->gLock = &gLock;
myArgs->yLock = &yLock;
myArgs->funcLock = &funcLock;
myArgs->startLock = &startLock;
myArgs->bufferAlpha = bufferAlpha;
myArgs->bufferBeta = bufferBeta;
myArgs->bufferIgrek = bufferIgrek;
myArgs->bufferGamma = bufferGamma;
myArgs->bufferFunc = bufferFunc;
myArgs->mutex = &mutex;
myArgs->isSolved = &isSolved;
}
IntegralSolver::~IntegralSolver()
{
fclose(file);
CloseHandle(mutex);
CloseHandle(isSolved);
delete[] bufferFunc;
delete myArgs;
}
unsigned int _stdcall IntegralSolver::ThreadCalculateAlpha(void* pParam)
{
Args* currentArgs = static_cast<Args*>(pParam);
double x;
int splitCount = (currentArgs->end - currentArgs->start) / currentArgs->dx;
for (size_t j = 0; j < splitCount; j++)
{
x = currentArgs->start + currentArgs->dx * j;
WAIT(currentArgs->startLock, 1);
for (size_t i = 0; i < 1000; i++)
{
double alpha = 0;
for (size_t k = 1; k < i; k++)
{
alpha += sin(x * 3.14 / 180.0);
}
currentArgs->bufferAlpha[i] = alpha;
}
FREE(currentArgs->aLock, 0);
}
return 0;
}
unsigned int _stdcall IntegralSolver::ThreadCalculateBeta(void* pParam)
{
Args* currentArgs = static_cast<Args*>(pParam);
double x;
int splitCount = (currentArgs->end - currentArgs->start) / currentArgs->dx;
for (size_t j = 0; j < splitCount; j++)
{
x = currentArgs->start + currentArgs->dx * j;
WAIT(currentArgs->aLock, 1);
for (size_t i = 0; i < 1000; i++)
{
long double beta = 0;
for (size_t k = 1; k < i; k++)
{
beta += currentArgs->bufferAlpha[i] * sin(x * 3.14 / 180.0);
}
currentArgs->bufferBeta[i] = beta;
}
FREE(currentArgs->bLock, 0);
}
return 0;
}
unsigned int _stdcall IntegralSolver::ThreadCalculateIgrek(void* pParam)
{
Args* currentArgs = static_cast<Args*>(pParam);
double x;
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.