Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <fstream>
- #include <vector>
- #include <time.h>
- #include <sstream>
- #include <math.h>
- using namespace std;
- struct myData {
- double a;
- double b;
- double eps;
- double delta;
- };
- int allThreadsTasks = 0;
- int noSolutions = 0;
- int mystakeTasks = 0;
- vector <myData> parametrs;
- vector <pair<myData, string>> results;
- int TestCount, ThreadCount;
- CRITICAL_SECTION cs1;
- CRITICAL_SECTION cs2;
- CRITICAL_SECTION cs3;
- CRITICAL_SECTION cs4;
- double f(double a, double b, double x) {
- return a*cos(x) / (b*x*x);
- }
- double df(double a, double b, double x) {
- return -(cos(x)*a * 2 / (b * x*x*x) + a*sin(x) / (b*x*x));
- }
- double d2f(double a, double b, double x) {
- return a*(-cos(x) / b + 4 * sin(x) / (3 * x) + 2 * cos(x) / (x*x)) / (x*x);
- }
- bool CheckSolutions(double a, double b, double i1, double i2) {
- if (f(a, b, i1)*f(a, b, i2) > 0) {
- if (abs(i1 - i2) < 0.001) {
- return false;
- }
- double c = (i1 + i2) / 2;
- CheckSolutions(a, b, i1, c);
- CheckSolutions(a, b, c, i2);
- }
- else {
- return true;
- }
- }
- class Thread {
- public:
- HANDLE tr;
- int ID;
- int tasks;
- boolean status;
- double maxTime;
- double minTime;
- double allTime;
- Thread() {
- ID = 0;
- tasks = 0;
- maxTime = 0;
- minTime = 100;
- allTime = 0;
- status = false;
- }
- int rez(myData data) {
- LARGE_INTEGER liC_S, liC_E, liF;
- QueryPerformanceCounter(&liC_S);
- QueryPerformanceFrequency(&liF);
- double i1 = 0, i2 = 100;
- double xk = data.delta;
- double xk_1 = 0;
- string str;
- pair <myData, string> p;
- p.first = data;
- bool flag = true;
- if (data.b == 0 || data.delta == 0) {
- mystakeTasks++;
- p.second = "Error in solution";
- flag = false;
- }
- if (!CheckSolutions(data.a, data.b, i1, i2) && flag) {
- flag = false;
- p.second = "No solutions";
- noSolutions++;
- }
- while (abs(xk - xk_1) > data.eps && flag) {
- xk_1 = xk;
- if (df(data.a, data.b, xk_1) == 0) {
- mystakeTasks++;
- p.second = "Error in solution";
- flag = false;
- break;
- }
- xk = xk_1 - (f(data.a, data.b, xk_1) / df(data.a, data.b, xk_1));
- }
- if (flag) {
- stringstream ss;
- ss << xk;
- ss >> p.second;
- EnterCriticalSection(&cs3);
- allThreadsTasks++;
- LeaveCriticalSection(&cs3);
- tasks++;
- }
- QueryPerformanceCounter(&liC_E);
- double time = ((double)liC_E.QuadPart / (double)liF.QuadPart) - ((double)liC_S.QuadPart / (double)liF.QuadPart);
- if (maxTime < time) {
- maxTime = time;
- }
- if (minTime > time) {
- minTime = time;
- }
- allTime += time;
- EnterCriticalSection(&cs2);
- results.push_back(p);
- LeaveCriticalSection(&cs2);
- return 0;
- }
- };
- vector <Thread> threads;
- DWORD WINAPI resolve(LPVOID i) {
- while (true) {
- bool flag = false;
- myData data;
- if (parametrs.size() == 0) {
- break;
- }
- EnterCriticalSection(&cs1);
- if (parametrs.size() != 0) {
- data = parametrs[0];
- parametrs.erase(parametrs.begin());
- flag = true;
- }
- LeaveCriticalSection(&cs1);
- if (flag) {
- threads[(int)i].rez(data);
- }
- }
- threads[(int)i].status = false;
- return 0;
- }
- bool fl;
- DWORD WINAPI ch(LPVOID p) {
- cout << "1";
- while (fl) {
- EnterCriticalSection(&cs4);
- for (int i = 0; i < ThreadCount; i++) {
- cout << threads[i].ID << " " << threads[i].tasks << " ";
- if (threads[i].status) {
- cout << "работает" << endl;
- }
- else
- cout << "не работает" << endl;
- }
- LeaveCriticalSection(&cs1);
- Sleep(1000);
- }
- return 0;
- }
- int main() {
- srand((unsigned int)time(NULL));
- setlocale(LC_ALL, ".1251");
- InitializeCriticalSection(&cs1);
- InitializeCriticalSection(&cs2);
- InitializeCriticalSection(&cs3);
- InitializeCriticalSection(&cs4);
- //int TestCount, ThreadCount;
- cout << "TestCount = ";
- cin >> TestCount;
- cout << endl << "ThreadCount = ";
- cin >> ThreadCount;
- cout << endl;
- DWORD IDThread;
- for (int i = 0; i < TestCount; i++) {
- double a = rand() % 100;
- double b = rand() % 100;
- double eps = (rand() % 100) / 100000.0;
- double delta = (rand() % 10000) / 100;
- myData data;
- data.a = a;
- data.b = b;
- data.eps = eps;
- data.delta = delta;
- parametrs.push_back(data);
- }
- for (int i = 0; i < ThreadCount; i++) {
- Thread a;
- threads.push_back(a);
- threads[i].ID = i;
- }
- fl = true;
- HANDLE h = new HANDLE;
- h = CreateThread(NULL, 0, ch, NULL, 0, &IDThread);
- Sleep(1000);
- HANDLE *handles = new HANDLE[ThreadCount];
- int i = 0;
- while (i<ThreadCount) {
- handles[i] = threads[i].tr = CreateThread(NULL, 0, resolve, (void*)threads[i].ID, 0, &IDThread);
- threads[i].status = true;
- if (threads[i].tr == NULL)
- return GetLastError();
- i++;
- }
- WaitForMultipleObjects(ThreadCount, handles, TRUE, INFINITE);
- fl = false;
- Sleep(1000);
- for (int i = 0; i < ThreadCount; ++i) {
- CloseHandle(threads[i].tr);
- }
- CloseHandle(h);
- DeleteCriticalSection(&cs1);
- DeleteCriticalSection(&cs2);
- DeleteCriticalSection(&cs3);
- DeleteCriticalSection(&cs4);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement