fork download
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<pthread.h>
  4.  
  5. pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
  6. int done=0;
  7. pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
  8.  
  9. void thr_exit(){
  10. pthread_mutex_lock(&mutex);
  11. done=1;
  12. pthread_cond_signal(&cond);
  13. usleep(20000);
  14. pthread_mutex_unlock(&mutex);
  15. printf("Thread exit\n");
  16. }
  17.  
  18. void *child(void *args){
  19. printf("Child\n");
  20. thr_exit();
  21. return NULL;
  22. }
  23. void thr_join(){
  24. pthread_mutex_lock(&mutex);
  25. while(done==0){
  26. pthread_cond_wait(&cond,&mutex);
  27. }
  28. pthread_mutex_unlock(&mutex);
  29. }
  30. int main(){
  31. pthread_t p;
  32. printf("Parent begin\n");
  33. pthread_create(&p,NULL,child,NULL);
  34. thr_join();
  35. printf("Parent end\n");
  36. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Parent  begin
Child
Thread exit
Parent end