Advertisement
Guest User

primos com threads

a guest
May 31st, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. /* LUIZ FELIPE MERIZIO  TRIDAPALLI */
  2.  
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <math.h>
  6. #include "pthread.h"
  7. #include <time.h>
  8. #include <windows.h>
  9.  
  10. pthread_mutex_t meu_mutex;
  11.  
  12. long int N = 5;
  13. long int total = 2;
  14. long int ultimo = 3;
  15. clock_t tempo;
  16.  
  17. bool primo(long int num){
  18.  
  19.     int cont = 0;
  20.     if (num %2 == 0){
  21.         return false;
  22.     }   else {
  23.             for (int i = 3 ; i < num; i+=2, cont++)
  24.         if (num % i == 0) {
  25.           return true;
  26.         } else {
  27.         return false;
  28.         }
  29.     }
  30.  
  31. }
  32.  
  33. void* encontra_primos(void *param )
  34. {
  35.  
  36.     while ( clock() - tempo <= 60000){
  37.  
  38.         if (primo(N) == true && N != ultimo){
  39.                 total++;
  40.                 ultimo = N;
  41.             }
  42.         N+=2;
  43.  
  44. }
  45.  
  46.      pthread_exit(NULL);
  47. }
  48.  
  49. void primosingle(){
  50.     N = 5;
  51.     total = 2;
  52.     ultimo = 3;
  53.  
  54.     printf("\nEncontrando primos single...");
  55.  
  56.     while (clock() - tempo <= 60000){
  57.        if (primo(N)){
  58.            total++;
  59.            ultimo = N;
  60.        }
  61.        N=N+2;
  62.     }
  63.  
  64.     printf("\n\tTotal de %d sem thread!", total );
  65.     printf("\n\tUltimo %d \n\n", ultimo );
  66.  
  67. }
  68.  
  69. void primomulti(){
  70.  
  71.     N = 5;
  72.     total = 2;
  73.     ultimo = 3;
  74.     tempo = clock();
  75.     pthread_t thd1, thd2, thd3, thd4;
  76.  
  77.     printf("\n\nEncontrando primos multithread...");
  78.  
  79.  
  80.     tempo = clock();
  81.  
  82.     pthread_mutex_init( &meu_mutex, NULL );
  83.  
  84.     pthread_create(&thd1,NULL,encontra_primos,NULL);
  85.  
  86.     pthread_create(&thd2,NULL,encontra_primos,NULL);
  87.  
  88.     pthread_create(&thd3,NULL,encontra_primos,NULL);
  89.  
  90.     pthread_create(&thd4,NULL,encontra_primos,NULL);
  91.     pthread_join( thd1, NULL );
  92.     pthread_join( thd2, NULL );
  93.     pthread_join( thd3, NULL );
  94.     pthread_join( thd4, NULL );
  95.  
  96.     pthread_mutex_destroy( &meu_mutex );
  97.  
  98.     printf("\n\tTotal de %d com thread!", total );
  99.     printf("\n\tUltimo %d \n\n", ultimo );
  100.  
  101. }
  102.  
  103. int main()
  104. {
  105.     char opcao;
  106.     printf("\n\t\tENCONTRA PRIMOS\n\n");
  107.     printf("--> Tecle S para Single ou M para Multi:   ");
  108.     scanf("%c", &opcao);
  109.  
  110.     if (opcao == 'S' || opcao == 's'){
  111.         primosingle();
  112.     } else if (opcao == 'M' || opcao == 'm'){
  113.         primomulti();
  114.     } else {
  115.     printf("\n\t\tOpcao incorreta!\n\n");
  116.     }
  117.  
  118.     system("Pause");
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement