fork download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <time.h>
  4.  
  5. #define NUM_THREADS 4
  6. #define ITERATIONS 1000000
  7.  
  8. long counters[NUM_THREADS];
  9.  
  10. void* increment(void* arg) {
  11. int id = *(int*)arg;
  12. for (int i = 0; i < ITERATIONS; i++) {
  13. counters[id]++;
  14. }
  15. return NULL;
  16. }
  17.  
  18. int main() {
  19. pthread_t threads[NUM_THREADS];
  20. int ids[NUM_THREADS];
  21.  
  22. clock_t start = clock();
  23.  
  24. for (int i = 0; i < NUM_THREADS; i++) {
  25. ids[i] = i;
  26. pthread_create(&threads[i], NULL, increment, &ids[i]);
  27. }
  28.  
  29. for (int i = 0; i < NUM_THREADS; i++) {
  30. pthread_join(threads[i], NULL);
  31. }
  32.  
  33. clock_t end = clock();
  34.  
  35. printf("Time (normal): %f seconds\n",
  36. (double)(end - start) / CLOCKS_PER_SEC);
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Time (normal): 0.000654 seconds