Slmaan

Untitled

Apr 25th, 2022 (edited)
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. /*/
  2.     |pthread example
  3.     |Name : slmaan albeady
  4.     |3.1 Programming with pthreads
  5. /*/
  6.  
  7.  
  8. #include <pthread.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #define RED   "\x1B[31m"
  13. #define CYN   "\x1B[36m"
  14. #define RESET "\x1B[0m"
  15.  
  16. void *thread_1(void * arg){
  17.     sleep(5);
  18.     printf(RED"Hello,I am thread 1\n"RESET);
  19.     return NULL;
  20. }
  21. void *thread_2(void * arg){
  22.     sleep(5);
  23.     printf(RED"Hello,I am thread 2\n"RESET);
  24.     return NULL;
  25. }
  26. int main(){
  27.     pthread_t i1 , i2 ;
  28.     pthread_create( &i1 , NULL, thread_1 , NULL);
  29.     pthread_create( &i2 , NULL, thread_2 , NULL);
  30.     pthread_join( i1 , NULL);
  31.     pthread_join( i2 , NULL);
  32.     printf(CYN"Hello,I am main process\n"RESET);
  33. }
  34. /*
  35. salbeady@penguin:~/os$ gcc -o test  pthread_example.c -lpthread
  36. salbeady@penguin:~/os$ ./test
  37. Hello,I am thread 2
  38. Hello,I am thread 1
  39. Hello,I am main process
  40. salbeady@penguin:~/os$
  41. */
Advertisement
Add Comment
Please, Sign In to add comment