fork(1) download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. int x = 5;
  5.  
  6. int main() {
  7. printf("=== BEFORE PARALLEL REGION ===");
  8. printf("\nGlobal x address: %p", (void*)&x);
  9. printf("\nGlobal x value: %d\n", x);
  10.  
  11. #pragma omp parallel sections private(x)
  12. {
  13. #pragma omp section
  14. {
  15. printf("\n x address: %p", (void*)&x);
  16. printf("\n x value BEFORE init: %d (garbage)", x);
  17.  
  18. x = 5;
  19. printf("\n x value AFTER init: %d", x);
  20.  
  21. x++;
  22. printf("\n x value AFTER increment: %d", x);
  23. printf("\n x=%d from first (private)", x);
  24. }
  25.  
  26. #pragma omp section
  27. {
  28. printf("\n x address: %p", (void*)&x);
  29. printf("\n x value BEFORE init: %d (garbage)", x);
  30.  
  31. x = 5;
  32. printf("\n x value AFTER init: %d", x);
  33.  
  34. x--;
  35. printf("\n x value AFTER decrement: %d", x);
  36. printf("\n x=%d from second (private)", x);
  37. }
  38. }
  39.  
  40. printf("\n\n=== AFTER PARALLEL REGION ===");
  41. printf("\nGlobal x address: %p", (void*)&x);
  42. printf("\nGlobal x value: %d", x);
  43. printf("\n\n");
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
=== BEFORE PARALLEL REGION ===
Global x address: 0x55f9808f1010
Global x value: 5

  x address: 0x55f9808f1010
  x value BEFORE init: 5 (garbage)
  x value AFTER init: 5
  x value AFTER increment: 6
  x=6 from first (private)
  x address: 0x55f9808f1010
  x value BEFORE init: 6 (garbage)
  x value AFTER init: 5
  x value AFTER decrement: 4
  x=4 from second (private)

=== AFTER PARALLEL REGION ===
Global x address: 0x55f9808f1010
Global x value: 4