fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6. int cnt = 0;
  7. //pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  8.  
  9. void *fun(void *arg) {
  10. printf("Hello, you are in thread\n");
  11. for (int i = 0; i < 1000; i++) {
  12. // pthread_mutex_lock(&mutex);
  13. cnt++;
  14. //pthread_mutex_unlock(&mutex);
  15. }
  16. // pthread_exit(NULL);
  17. }
  18.  
  19. int main() {
  20. pthread_t t1, t2;
  21.  
  22. pthread_create(&t1, NULL, fun, NULL);
  23. pthread_create(&t2, NULL, fun, NULL);
  24.  
  25. pthread_join(t1, NULL);
  26. pthread_join(t2, NULL);
  27.  
  28. printf("Hello, you are in main\n");
  29. printf("The total count is: %d\n", cnt);
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Hello, you are in thread
Hello, you are in thread
Hello, you are in main
The total count is: 2000