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[10000000][2];
- long TaskCount;
- long ThreadCount;
- long Tasks;
- //-------------------------------
- struct Matrix {
- double matrix[5][2];
- bool check;
- int doneBy;
- int answer;
- int solved;
- bool newT;
- double time;
- Matrix(){
- check = false;
- newT = false;
- }
- Matrix(double m[5][2]){
- check = false;
- newT = false;
- 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;
- Arg(Matrix *m){
- matr = new Matrix[TaskCount];
- matr = m;
- }
- };
- 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){
- int curr = ((Arg*)arg)->id;
- int i = Tasks;
- while(i < TaskCount){
- Sleep(10);
- if(((Arg *)arg)->matr[i].check == false){
- WaitForSingleObject( hMutex, INFINITE );
- LARGE_INTEGER l1, l2, l3;
- QueryPerformanceCounter(&l1);
- QueryPerformanceFrequency(&l3);
- if(checker(((Arg *)arg)->matr[i].matrix) == 1){
- ((Arg *)arg)->matr[i].check = true;
- //-----------------
- stat[i][0] = curr;
- stat[i][1] = 1;
- ((Arg *)arg)->matr[i].answer = 1;
- ((Arg *)arg)->matr[i].doneBy = curr;
- ((Arg *)arg)->matr[i].solved = 1;
- cout << curr << " Solved" << endl;
- //-------------------
- }
- else if(((Arg *)arg)->matr[i].check == 0){
- ((Arg *)arg)->matr[i].check = true;
- //-----------------
- stat[i][0] = curr;
- stat[i][1] = 0;
- ((Arg *)arg)->matr[i].answer = 0;
- ((Arg *)arg)->matr[i].doneBy = curr;
- ((Arg *)arg)->matr[i].solved = 1;
- cout << curr << " Solved" << endl;
- //-------------------
- }
- QueryPerformanceCounter(&l2);
- double totalTime = ((double)l2.QuadPart / (double)l3.QuadPart) - ((double)l1.QuadPart / (double)l3.QuadPart);//итоговое время
- ((Arg *)arg)->matr[i].time = totalTime;
- ReleaseMutex( hMutex );
- }
- i++;
- Tasks = i;
- if(i >= TaskCount && hasTasks(((Arg *)arg)->matr)){
- i = 0;
- Tasks = i;
- }
- }
- 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;
- }
- DWORD WINAPI myCheck(void *args){
- while(true){
- for(int i = 0;i < TaskCount;i++){
- if(((Arg *)args)->matr[i].check == true && ((Arg *)args)->matr[i].newT == false){
- cout << ((Arg *)args)->matr[i].doneBy << " Solved task number " << i << endl;
- ((Arg *)args)->matr[i].newT = true;
- }
- }
- //Sleep(1);
- }
- return 0;
- }
- int main(){
- Tasks = 0;
- hMutex=CreateMutex( NULL, FALSE, NULL );
- TaskCount = 0;
- cout << "Enter Task Count" << endl;
- cin >> TaskCount;
- Matrix *m = new Matrix[TaskCount];
- generator(m);
- //----------------------------------
- cout << "Enter ThreadCount" << endl;
- ThreadCount = 0;
- cin >> ThreadCount;
- HANDLE *hThread = new HANDLE[ThreadCount];
- DWORD *IDThread = new DWORD[ThreadCount];
- Arg args(m);
- HANDLE hCheck;
- DWORD idCheck;
- //hCheck = CreateThread(NULL, 0, myCheck, &args, 0, &idCheck);
- for(int i = 0;i < ThreadCount;i++){
- args.id = i;
- hThread[i] = CreateThread(NULL, 0, myThread, &args, 0, &IDThread[i]);
- Sleep(0.0005);
- 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 = " << m[k].answer << endl;
- for(int i = 0;i < 5;i++){
- for(int j = 0;j < 2;j++){
- fout << m[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;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement