Advertisement
Caio_25

Thread_numeros_primos

Apr 11th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 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 10000
  15. #define threads 4
  16.  
  17. typedef struct{
  18.     int li, lf, ci, cf, soma;
  19. }par;
  20.  
  21. int mat[dim][dim];
  22.  
  23. void preencher_matriz();
  24. void qtd_primos(void *parThread);
  25. bool eh_primo(int x);
  26.  
  27. int main()
  28. {
  29.     srand (time(NULL));
  30.    
  31.     DWORD ThreadIdentifier[threads];
  32.     HANDLE idThread[threads];
  33.     par parametro[threads];
  34.     //clock_t inicio, fim;
  35.     int total_primos = 0;
  36.  
  37.     preencher_matriz();
  38.  
  39.     for(int i = 0; i < threads; i++)
  40.     {
  41.         parametro[i].li = 0;
  42.         parametro[i].lf = dim;
  43.         parametro[i].ci = 0 + (dim/threads)*i;
  44.         parametro[i].cf = (dim/threads) + (dim/threads)*i;
  45.         parametro[i].soma = 0;
  46.  
  47.     }
  48.  
  49.     for(int i = 0; i < threads; i++)
  50.     {
  51.         idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)qtd_primos, &parametro[i], 0, &ThreadIdentifier[i]);
  52.  
  53.     }
  54.  
  55.     WaitForMultipleObjects(threads, idThread, TRUE, INFINITE);
  56.  
  57.     //WaitForSingleObject(idThread[1], INFINITE);
  58.  
  59.    
  60.  
  61.     for(int i = 0; i < threads; i++)
  62.     {
  63.  
  64.         CloseHandle(idThread[i]);
  65.  
  66.     }
  67.  
  68.     for(int i = 0; i < threads; i++)
  69.     {
  70.         total_primos += parametro[i].soma;
  71.     }
  72.  
  73.     printf("Qtd de primos: %d\n", total_primos);
  74.  
  75.    
  76.    
  77.     getchar();
  78.    
  79.     return 0;
  80. }
  81.  
  82.  
  83. void preencher_matriz()
  84. {
  85.     for(int i = 0; i < dim; i++)
  86.     {
  87.         for(int j = 0; j < dim; j++)
  88.         {
  89.             mat[i][j] = rand()%1000000;
  90.         }
  91.     }
  92. }
  93.  
  94. void qtd_primos(void *parThread)
  95. {
  96.     par *parFuncao = (par *) parThread;
  97.  
  98.    
  99.     printf("linha_inicial: %d\tlinha final: %d\n", parFuncao->li, parFuncao->lf);
  100.     printf("coluna_inicial: %d\tcoluna final: %d\n", parFuncao->ci, parFuncao->cf);
  101.     for(int i = parFuncao->li; i < parFuncao->lf; i++)
  102.     {
  103.         for(int j = parFuncao->ci; j < parFuncao->cf; j++)
  104.         {
  105.  
  106.             parFuncao->soma += eh_primo(mat[i][j]);
  107.         }
  108.     }
  109.  
  110. }
  111.  
  112. bool eh_primo(int x)
  113. {
  114.     if(x % 2 == 0 && x!=2)
  115.     {
  116.         return(0);
  117.     }
  118.  
  119.     else if(x == 2)
  120.     {
  121.         return(1);
  122.     }
  123.  
  124.     else
  125.     {
  126.         int aux = 0;
  127.         for(int i = 1; i <= x; i+=2)
  128.         {
  129.             if(x % i == 0)
  130.             {
  131.                 aux++;
  132.             }
  133.  
  134.             if(aux >= 3)
  135.             {
  136.                 return(0);
  137.             }
  138.  
  139.         }
  140.  
  141.         if(aux == 2)
  142.         {
  143.             return(1);
  144.         }
  145.  
  146.         else
  147.         {
  148.             return(0);
  149.         }
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement