Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <windows.h>
- #include <ctime>
- #include <vector>
- using namespace std;
- ofstream fout("out.txt");
- HANDLE hMutex;
- int **stat;
- long TaskCount;
- long ThreadCount;
- long Tasks;
- //-------------------------------
- struct Matrix {
- double matrix[5][2];
- bool check;
- int doneBy;
- int answer;
- int solved;
- double time;
- Matrix(){
- check = false;
- doneBy = -1;
- answer = -1;
- solved = -1;
- time = 0;
- }
- Matrix(double m[5][2]){
- check = false;
- check = false;
- doneBy = -1;
- answer = -1;
- solved = -1;
- time = 0;
- for(int i = 0;i < 5;i++){
- for(int j = 0;j < 2;j++){
- matrix[i][j] = m[i][j];
- }
- }
- }
- };
- struct Arg{
- Matrix *matr;
- int id;
- };
- void generator(Matrix *m){
- srand(time(NULL));
- double matrixTask[5][2];
- for(int i = 0;i < TaskCount;i++){
- matrixTask[0][0] = rand() % 6;
- matrixTask[0][1] = rand() % 6;
- matrixTask[1][0] = 5 + rand() % 6;
- matrixTask[1][1] = rand() % 3;
- matrixTask[2][0] = 5 + rand() % 11;
- matrixTask[2][1] = rand() % 11;
- matrixTask[3][0] = 5 + rand() % 6;
- matrixTask[3][1] = 7 + rand() % 4;
- matrixTask[4][0] = rand() % 6;
- matrixTask[4][1] = 5 + rand() % 6;
- Matrix m1(matrixTask);
- m[i] = m1;
- }
- }
- void showMatr(Matrix *m){
- for(int i = 0;i < TaskCount;i++){
- for(int k = 0;k < 5;k++){
- for(int j = 0;j < 2;j++){
- cout << m[i].matrix[k][j] << " ";
- }
- cout << endl;
- }
- cout << endl << endl;
- }
- }
- bool hasTasks(Matrix *m){
- for(int i = 0;i < TaskCount;i++){
- if(m[i].check == false){
- return true;
- }
- }
- return false;
- }
- int checker(double c[5][2]){
- double matrix[5][2];
- double res[5];
- for(int i = 0; i < 5;i++){
- if(i == 4){
- matrix[i][0] = c[0][0] - c[i][0];
- matrix[i][1] = c[0][1] - c[i][1];
- }
- else{
- matrix[i][0] = c[i + 1][0] - c[i][0];
- matrix[i][1] = c[i + 1][1] - c[i][1];
- }
- }
- //---------------------
- double T = matrix[4][0] * matrix[0][1] - matrix[4][1] * matrix[0][0];
- T = T/abs(T);
- double P = 1;
- for(int i = 0;i < 5;i++){
- double R = matrix[i][0] * matrix[i + 1][1] - matrix[i][1] * matrix[i + 1][0];
- P = T * P * R/abs(R);
- if(P < 0)
- return 0;
- }
- return 1;
- }
- DWORD WINAPI myThread(void *arg){
- Arg *a = ((Arg*)arg);
- int curr = a->id;
- int taskNum = 0;
- double task[5][2];
- while(hasTasks(a->matr)){
- WaitForSingleObject( hMutex, INFINITE );
- for(int i = 0; i < TaskCount;i++){
- if(a->matr[i].check == false){
- a->matr[i].check = true;
- taskNum = i;
- for(int k = 0;k < 5;k++){
- for(int j = 0; j < 2;j++){
- task[k][j] = a->matr[i].matrix[k][j];
- }
- }
- a->matr[i].doneBy = curr;
- break;
- }
- }
- ReleaseMutex( hMutex );
- int answer,solved;
- LARGE_INTEGER l1, l2, l3;
- QueryPerformanceCounter(&l1);
- QueryPerformanceFrequency(&l3);
- if(checker(task) == 1){
- answer = 1;
- solved = 1;
- }
- else if(checker(task) == 0){
- answer = 0;
- solved = 1;
- }
- QueryPerformanceCounter(&l2);
- double totalTime = ((double)l2.QuadPart / (double)l3.QuadPart) - ((double)l1.QuadPart / (double)l3.QuadPart);//итоговое время
- WaitForSingleObject( hMutex, INFINITE );
- stat[taskNum][0] = curr;
- stat[taskNum][1] = answer;
- a->matr[taskNum].answer = answer;
- a->matr[taskNum].solved = solved;
- a->matr[taskNum].time = totalTime;
- ReleaseMutex( hMutex );
- }
- return 0;
- }
- void solvedN(Matrix *m){
- int counter = 0;
- for(int i = 0;i < TaskCount;i++){
- if(m[i].answer == 0) counter++;
- }
- cout << "Tasks that havent solutions = " << counter << endl;
- }
- void threads(Matrix *m){
- int totalTasks = 0;
- for(int i = 0;i < ThreadCount;i++){
- int counter = 0;
- double time = 0;
- for(int j = 0; j < TaskCount;j++){
- if(i == m[j].doneBy){
- counter++;
- time += m[j].time;
- }
- }
- cout << "Thread with ID = " << i << " solved = " << counter << " tasks. Total time = " << time << endl;
- totalTasks += counter;
- }
- cout << "Total Solved Tasks = " << totalTasks << endl;
- }
- void minMax(Matrix *m){
- double min = 10;
- double max = 0;
- for(int i = 0; i < TaskCount;i++){
- if(m[i].time < min && m[i].time != 0){
- min = m[i].time;
- }
- if(m[i].time > max){
- max = m[i].time;
- }
- }
- if(min == 10)
- min = max;
- cout << "Max time for solving = " << max << endl << "Min time for solving = " << min << endl;
- }
- int main(){
- Tasks = 0;
- hMutex=CreateMutex( NULL, FALSE, NULL );
- TaskCount = 0;
- cout << "Enter Task Count" << endl;
- while(TaskCount <= 0)
- cin >> TaskCount;
- stat = new int*[TaskCount];
- for(int i = 0 ; i < TaskCount;i++){
- stat[i] = new int[2];
- }
- //----------------------------------
- cout << "Enter ThreadCount" << endl;
- ThreadCount = 0;
- while(ThreadCount <= 0)
- cin >> ThreadCount;
- Arg args;
- Matrix *m = new Matrix[TaskCount];
- generator(m);
- args.matr = m;
- HANDLE *hThread = new HANDLE[ThreadCount];
- DWORD *IDThread = new DWORD[ThreadCount];
- for(int i = 0;i < ThreadCount;i++){
- args.id = i;
- hThread[i] = CreateThread(NULL, 0, myThread, &args, 0, &IDThread[i]);
- Sleep(0.5);
- if (hThread[i] == NULL){
- cout<<"Thread doesnt exist"<<endl;
- return GetLastError();
- }
- }
- if (WaitForMultipleObjects(ThreadCount, hThread, TRUE, INFINITE)) {
- cout << "Wait for multiple objects failed" << endl;
- }
- for(int i = 0;i < ThreadCount;i++){
- CloseHandle(hThread[i]);
- }
- //--------------------------
- double writeTimeBeg = clock();
- for(int k = 0;k < TaskCount;k++){
- fout <<"Answer = " << args.matr[k].answer << endl;
- for(int i = 0;i < 5;i++){
- for(int j = 0;j < 2;j++){
- fout << args.matr[k].matrix[i][j] << " ";
- }
- fout << endl;
- }
- fout << "---------------" <<endl;
- }
- double writeTimeEnd = clock();
- //--------------------------
- solvedN(args.matr);
- threads(args.matr);
- minMax(args.matr);
- cout << "Time for writting data = " << ((writeTimeEnd - writeTimeBeg) / CLOCKS_PER_SEC) << endl;
- delete[] m;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement