Advertisement
PadmaJS

Untitled

May 24th, 2025
1,727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <semaphore.h>
  4.  
  5. sem_t sem_hello_done;
  6.  
  7. void* print_hello(void* arg) {
  8.     printf("Hello ");
  9.     sem_post(&sem_hello_done);
  10.     return NULL;
  11. }
  12.  
  13. void* print_world(void* arg) {
  14.     sem_wait(&sem_hello_done);
  15.     printf("World\n");
  16.     return NULL;
  17. }
  18.  
  19. int main() {
  20.     pthread_t t1, t2;
  21.  
  22.     sem_init(&sem_hello_done, 0, 0);
  23.  
  24.     pthread_create(&t1, NULL, print_hello, NULL);
  25.     pthread_create(&t2, NULL, print_world, NULL);
  26.  
  27.     pthread_join(t1, NULL);
  28.     pthread_join(t2, NULL);
  29.  
  30.     sem_destroy(&sem_hello_done);
  31.  
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement