Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <thread>
  5. using namespace std;
  6.  
  7. int bitmap[1010][1010];
  8. int cont=0;
  9. void* findBit(void *tasks){ //função de thread recursiva
  10.     int *instructions=(int *)tasks;
  11.     int inil=instructions[0],inic=instructions[1],l=instructions[2],c=instructions[3];
  12.     int i, j, zero=0, one=0;
  13.     for(i=inil;i<=l;i++){ // confiro se é tudo 1, 0, misturado, ou vazio
  14.         for(j=inic;j<=c;j++){
  15.             if(bitmap[i][j]==1) one++;
  16.             else zero++;
  17.         }
  18.     }
  19.     string *str_ptr = new string;
  20.     if(zero && one){ //se for 0 e 1 crio outras 4 threads que vão analiza os novos 4 quadrantes
  21.         *str_ptr = "D";// antes de tudo salvo na string resposta o "D"
  22.         int lm = (inil+l)>>1; //vejo os limites de linhas e colunas pra recursão
  23.         int cm = (inic+c)>>1;
  24.         pthread_t threads[4];
  25.         int inst[]={inil, inic, lm, cm}; // declaro os argumentos das 4 novas threads
  26.         int inst2[]={inil, cm+1, lm, c};
  27.         int inst3[]={lm+1, inic, l, cm};
  28.         int inst4[]={lm+1, cm+1, l, c};
  29.         pthread_create(&threads[0],NULL,findBit, (void *)inst); //crio as 4 threads
  30.         pthread_create(&threads[1],NULL,findBit, (void *)inst2);
  31.         pthread_create(&threads[2],NULL,findBit, (void *)inst3);
  32.         pthread_create(&threads[3],NULL,findBit, (void *)inst4);
  33.         string *str_ptr_aux; //crio um ponteiro de string auxiliar para receber o retorno das 4 threads que criei
  34.         for(int k=0;k<4;k++){
  35.             pthread_join(threads[k], (void**)&str_ptr_aux); // espero com join as 4 threads e salvo o retorno delas em str_prt_aux
  36.             *str_ptr += *str_ptr_aux; //concateno o valor que o join retornou na string resposta
  37.         }    
  38.         pthread_exit(str_ptr); //retorno a string resposta, seja para o main ou para outra thread que a tenha chamado
  39.     }else if(one){ // se for tudo um retorno "1" como string resposta para o main ou para outra a thread que a tenha chamado
  40.         *str_ptr = "1";
  41.         pthread_exit(str_ptr);
  42.     }else if(zero){ // se for tudo um retorno "0" como string resposta para o main ou para outra a thread que a tenha chamado
  43.         *str_ptr = "0";
  44.         pthread_exit(str_ptr);
  45.     }else{ // se for vazia retorno uma string vazia como resposta para a main ou a thread que a tenha chamado
  46.         *str_ptr = "";
  47.         pthread_exit(str_ptr);
  48.     }
  49. }
  50.  
  51. int main(){
  52.     pthread_t thread;
  53.     int l, c, i, j;
  54.     char s[101010];
  55.     printf("Diga o numero de linhas e colunas -> ");
  56.     scanf("%d %d", &l, &c);
  57.     printf("Digite o Bit Map:\n");
  58.     for(i=0;i<l;i++){ //leio o meu bitmap
  59.         scanf("%s", s);
  60.         for(j=0;j<c;j++){
  61.             int conv=s[j]-'0';
  62.             bitmap[i+1][j+1]=conv;
  63.         }
  64.     }
  65.     int tasks[] = {1,1,l,c}; //escolho os valores do argumento
  66.     pthread_create(&thread,NULL,findBit, (void *)tasks); //crio a thread
  67.     string *str_ptr; // ponteiro de string que vai receber a resposta
  68.     pthread_join(thread, (void**)&str_ptr); //espero ela acabar
  69.     printf("A resposta eh -> ");
  70.     cout << *str_ptr << endl; // printo a resposta
  71.     pthread_exit(NULL);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement