Jeremiah_

SO P&T - 4

Sep 30th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <sys/types.h>
  6.  
  7. char ch = 'a';
  8. pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  9.  
  10.  
  11. void* print_ch(void*value){
  12.     pthread_mutex_lock(&mtx);
  13.     printf("%c", ch);
  14.     ch++;
  15.     pthread_mutex_unlock(&mtx);
  16.     return NULL;
  17. }
  18.  
  19. int main(){
  20.     pthread_t arr[3];
  21.    
  22.     int i;
  23.     for(i = 0; i < 3; i++){
  24.         pthread_create(&arr[i], NULL, &print_ch, NULL);
  25.     }
  26.    
  27.     for(i = 0; i < 3; i++){
  28.         pthread_join(arr[i], NULL);
  29.     }
  30.  
  31.     putchar('\n');
  32. }
Advertisement
Add Comment
Please, Sign In to add comment