Jeremiah_

SO tentativa trab 5

Oct 21st, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6.  
  7. #define NTHREAD 2
  8.  
  9. pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  10.  
  11. char *mensagem;
  12.  
  13. void* threadFunction(void* value){
  14.     pthread_mutex_lock(&mtx);
  15.     puts("thread: ");
  16.     mensagem = (char) pthread_self();
  17.     puts(mensagem);
  18.     pthread_mutex_unlock(&mtx);
  19. }
  20.  
  21.  
  22. int main ()
  23. {
  24.     pthread_t threads[NTHREAD];
  25.     printf("Comecando o programa threads1 ... \n");
  26.    
  27.     int i;
  28.     for(i=0; i<NTHREAD; i++){
  29.         pthread_create(&threads[i], NULL, threadFunction, NULL);
  30.     }
  31.  
  32.     for(i=0; i<NTHREAD; i++){
  33.         pthread_join(threads[i], NULL);
  34.     }
  35.  
  36.     printf("Terminando o programa threads1 ... \n");
  37.     system("ps -f");
  38.  
  39.     exit (0);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment