fork(1) download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3.  
  4. #define NUM_THREADS 4
  5.  
  6. int shared = 0;
  7.  
  8. void *foo()
  9. {
  10. for (int i = 0; i < 10000; ++i) {
  11. shared += 1;
  12. }
  13. return NULL;
  14. }
  15.  
  16. int main()
  17. {
  18. pthread_t threads[NUM_THREADS];
  19.  
  20. for (int i = 0; i < NUM_THREADS; ++i) {
  21. pthread_create(&threads[i], NULL, foo, NULL);
  22. }
  23.  
  24. for (int i = 0; i < NUM_THREADS; ++i) {
  25. pthread_join(threads[i], NULL);
  26. }
  27.  
  28. printf("final result is %d\n", shared);
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
final result is 40000