Advertisement
Caio_25

Thread3

Apr 19th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 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, indice;
  19. }par;
  20.  
  21. int mat[dim][dim];
  22. int total_numeros_primos = 0;
  23. HANDLE mSC1, hMutexVideo;
  24.  
  25. void preencher_matriz();
  26. void qtd_primos(void *parThread);
  27. bool eh_primo(int x);
  28. void SetParametros(par p[]);
  29. void CriarThreads(HANDLE idThread[], par parametro[], DWORD ThreadIdentifier[]);
  30. void SetPrioridade(HANDLE idThread[]);
  31. void MostrarPrioridade(HANDLE idThread[]);
  32. void PressEnter();
  33. void IniciarThreads(HANDLE idThread[]);
  34. void FecharThread(HANDLE idThread[]);
  35.  
  36. int main()
  37. {
  38.     srand (12);
  39.    
  40.     DWORD ThreadIdentifier[threads];
  41.     HANDLE idThread[threads];
  42.     par parametro[threads];
  43.     clock_t inicio, fim;
  44.     int total_primos = 0;
  45.  
  46.     inicio = clock();
  47.  
  48.     //criando Mutex
  49.     mSC1 = CreateMutex(NULL, FALSE, NULL);
  50.     hMutexVideo = CreateMutex(NULL, FALSE, NULL);
  51.  
  52.     preencher_matriz();
  53.     SetParametros(parametro);
  54.     CriarThreads(idThread, parametro, ThreadIdentifier);
  55.     SetPrioridade(idThread);
  56.     MostrarPrioridade(idThread);
  57.     PressEnter();
  58.     IniciarThreads(idThread);
  59.     WaitForMultipleObjects(threads, idThread, TRUE, INFINITE);
  60.     FecharThread(idThread);
  61.    
  62.  
  63.  
  64.     /*WaitForSingleObject(idThread[1], INFINITE);
  65.     for(int i = 0; i < threads; i++)
  66.     {
  67.         total_primos += parametro[i].soma;
  68.     }
  69.     printf("Qtd de primos: %d\n", total_primos);*/
  70.    
  71.    
  72.    
  73.     printf("total numeros primos: %d\n", total_numeros_primos);
  74.  
  75.    
  76.     fim = clock();
  77.     double time = ((double) (fim - inicio) ) / CLOCKS_PER_SEC;
  78.     printf("tempo: %lf\n", time);
  79.    
  80.    
  81.     system("pause");
  82.     return 0;
  83. }
  84.  
  85.  
  86. void preencher_matriz()
  87. {
  88.     for(int i = 0; i < dim; i++)
  89.     {
  90.         for(int j = 0; j < dim; j++)
  91.         {
  92.             mat[i][j] = rand()%1000000;
  93.         }
  94.     }
  95. }
  96.  
  97. void qtd_primos(void *parThread)
  98. {
  99.     par *parFuncao = (par *) parThread;
  100.  
  101.    WaitForSingleObject(hMutexVideo, INFINITE);
  102.     printf("linha_inicial: %d\tlinha final: %d\n", parFuncao->li, parFuncao->lf);
  103.     printf("coluna_inicial: %d\tcoluna final: %d\n\n", parFuncao->ci, parFuncao->cf);
  104.  
  105.     ReleaseMutex(hMutexVideo);
  106.    
  107.    
  108.    
  109.     //printf("Starting thread %d\n", parFuncao->indice);
  110.    
  111.  
  112.     for(int i = parFuncao->li; i < parFuncao->lf; i++)
  113.     {
  114.         for(int j = parFuncao->ci; j < parFuncao->cf; j++)
  115.         {
  116.  
  117.             // inicio seção crítica
  118.  
  119.             WaitForSingleObject(mSC1, INFINITE);
  120.  
  121.             total_numeros_primos += eh_primo(mat[i][j]);
  122.  
  123.            //parFuncao->soma += eh_primo(mat[i][j]);
  124.  
  125.             ReleaseMutex(mSC1);
  126.  
  127.             // fim seção crítica
  128.  
  129.         }
  130.     }
  131.  
  132. }
  133.  
  134. bool eh_primo(int x)
  135. {
  136.     if(x % 2 == 0 && x!=2)
  137.     {
  138.         return(0);
  139.     }
  140.  
  141.     else if(x == 2)
  142.     {
  143.         return(1);
  144.     }
  145.  
  146.     else
  147.     {
  148.         int aux = 0;
  149.         for(int i = 1; i <= x; i+=2)
  150.         {
  151.             if(x % i == 0)
  152.             {
  153.                 aux++;
  154.             }
  155.  
  156.             if(aux >= 3)
  157.             {
  158.                 return(0);
  159.             }
  160.  
  161.         }
  162.  
  163.         if(aux == 2)
  164.         {
  165.             return(1);
  166.         }
  167.  
  168.         else
  169.         {
  170.             return(0);
  171.         }
  172.     }
  173.  
  174. }
  175.  
  176. void SetParametros(par p[])
  177. {
  178.     for(int i = 0; i < threads; i++)
  179.     {
  180.         p[i].li = 0;
  181.         p[i].lf = dim;
  182.         p[i].ci = 0 + (dim/threads)*i;
  183.         p[i].cf = (dim/threads) + (dim/threads)*i;
  184.         //p[i].soma = 0;
  185.         p[i].indice = i;
  186.  
  187.     }
  188. }
  189.  
  190. void CriarThreads(HANDLE idThread[], par parametro[], DWORD ThreadIdentifier[])
  191. {
  192.     for(int i = 0; i < threads; i++)
  193.     {
  194.         idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)qtd_primos, &parametro[i], CREATE_SUSPENDED, &ThreadIdentifier[i]);
  195.  
  196.     }
  197. }
  198.  
  199. void SetPrioridade(HANDLE idThread[])
  200. {
  201.         SetThreadPriority(idThread[0], THREAD_PRIORITY_BELOW_NORMAL );
  202.     SetThreadPriority(idThread[2], THREAD_PRIORITY_HIGHEST );
  203. }
  204.  
  205. void MostrarPrioridade(HANDLE idThread[])
  206. {
  207.    
  208.     WaitForSingleObject(hMutexVideo, INFINITE);
  209.         printf("\n");
  210.         for(int i  = 0; i < threads; i++)
  211.         {
  212.             printf("thread %d priority %d\n", i, GetThreadPriority(idThread[i]));
  213.         }
  214.         printf("\n");
  215.     ReleaseMutex(hMutexVideo);
  216. }
  217.  
  218. void PressEnter()
  219. {
  220.     WaitForSingleObject(hMutexVideo, INFINITE);
  221.        
  222.         printf("Press enter to start the threads ");
  223.         getchar();
  224.     ReleaseMutex(hMutexVideo);
  225. }
  226.  
  227. void IniciarThreads(HANDLE idThread[])
  228. {
  229.         for(int i = 0; i < threads; i++)
  230.         {
  231.             ResumeThread(idThread[i]);
  232.         }
  233. }
  234.  
  235. void FecharThread(HANDLE idThread[])
  236. {
  237.     for(int i = 0; i < threads; i++)
  238.     {
  239.  
  240.         CloseHandle(idThread[i]);
  241.  
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement