Advertisement
Caio_25

thread2

Apr 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. // aula_thread.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include<Windows.h>
  5. #include<process.h>
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<time.h>
  9. #include<iostream>
  10. #include<vector>
  11.  
  12.  
  13.  
  14. #define dim 1000
  15. #define threads 4
  16.  
  17. typedef struct{
  18.     int li, lf, ci, cf;
  19. }par;
  20.  
  21. int mat[dim][dim];
  22. int total_numeros_primos = 0;
  23. HANDLE mSC1, mSC2;
  24.  
  25. void preencher_matriz();
  26. void qtd_primos(void *parThread);
  27. bool eh_primo(int x);
  28.  
  29. int main()
  30. {
  31.     srand (12);
  32.    
  33.     DWORD ThreadIdentifier[threads];
  34.     HANDLE idThread[threads];
  35.     par parametro[threads];
  36.     clock_t inicio, fim;
  37.     int total_primos = 0;
  38.  
  39.     inicio = clock();
  40.  
  41.     //criando Mutex
  42.     mSC1 = CreateMutex(NULL, FALSE, NULL);
  43.     mSC2 = CreateMutex(NULL, FALSE, NULL);
  44.  
  45.     preencher_matriz();
  46.  
  47.     for(int i = 0; i < threads; i++)
  48.     {
  49.         parametro[i].li = 0;
  50.         parametro[i].lf = dim;
  51.         parametro[i].ci = 0 + (dim/threads)*i;
  52.         parametro[i].cf = (dim/threads) + (dim/threads)*i;
  53.         //parametro[i].soma = 0;
  54.  
  55.     }
  56.  
  57.     for(int i = 0; i < threads; i++)
  58.     {
  59.         idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)qtd_primos, &parametro[i], 0, &ThreadIdentifier[i]);
  60.  
  61.     }
  62.  
  63.     WaitForMultipleObjects(threads, idThread, TRUE, INFINITE);
  64.  
  65.     //WaitForSingleObject(idThread[1], INFINITE);
  66.  
  67.    
  68.  
  69.     for(int i = 0; i < threads; i++)
  70.     {
  71.  
  72.         CloseHandle(idThread[i]);
  73.  
  74.     }
  75.  
  76.     /*for(int i = 0; i < threads; i++)
  77.     {
  78.         total_primos += parametro[i].soma;
  79.     }*/
  80.  
  81.     //printf("Qtd de primos: %d\n", total_primos);
  82.    
  83.     printf("total numeros primos: %d\n", total_numeros_primos);
  84.  
  85.    
  86.     fim = clock();
  87.  
  88.     double time = ((double) (fim - inicio) ) / CLOCKS_PER_SEC;
  89.  
  90.     printf("tempo: %lf\n", time);
  91.    
  92.     system("pause");
  93.    
  94.     return 0;
  95. }
  96.  
  97.  
  98. void preencher_matriz()
  99. {
  100.     for(int i = 0; i < dim; i++)
  101.     {
  102.         for(int j = 0; j < dim; j++)
  103.         {
  104.             mat[i][j] = rand()%1000000;
  105.         }
  106.     }
  107. }
  108.  
  109. void qtd_primos(void *parThread)
  110. {
  111.     par *parFuncao = (par *) parThread;
  112.  
  113.    WaitForSingleObject(mSC2, INFINITE);
  114.     printf("linha_inicial: %d\tlinha final: %d\n", parFuncao->li, parFuncao->lf);
  115.     printf("coluna_inicial: %d\tcoluna final: %d\n\n", parFuncao->ci, parFuncao->cf);
  116.  
  117.     ReleaseMutex(mSC2);
  118.  
  119.     for(int i = parFuncao->li; i < parFuncao->lf; i++)
  120.     {
  121.         for(int j = parFuncao->ci; j < parFuncao->cf; j++)
  122.         {
  123.  
  124.             // inicio seção crítica
  125.  
  126.             WaitForSingleObject(mSC1, INFINITE);
  127.  
  128.             total_numeros_primos += eh_primo(mat[i][j]);
  129.  
  130.            //parFuncao->soma += eh_primo(mat[i][j]);
  131.  
  132.             ReleaseMutex(mSC1);
  133.  
  134.             // fim seção crítica
  135.  
  136.         }
  137.     }
  138.  
  139. }
  140.  
  141. bool eh_primo(int x)
  142. {
  143.     if(x % 2 == 0 && x!=2)
  144.     {
  145.         return(0);
  146.     }
  147.  
  148.     else if(x == 2)
  149.     {
  150.         return(1);
  151.     }
  152.  
  153.     else
  154.     {
  155.         int aux = 0;
  156.         for(int i = 1; i <= x; i+=2)
  157.         {
  158.             if(x % i == 0)
  159.             {
  160.                 aux++;
  161.             }
  162.  
  163.             if(aux >= 3)
  164.             {
  165.                 return(0);
  166.             }
  167.  
  168.         }
  169.  
  170.         if(aux == 2)
  171.         {
  172.             return(1);
  173.         }
  174.  
  175.         else
  176.         {
  177.             return(0);
  178.         }
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement