-1

Initially value of ab is 10, then after some delay created by for loop ab is set to 55 and then its printed in this code..

#include <iostream>
using namespace std;
int main()
{
    long j, i; 
    int ab=10 ;

    for(i=0; i<1000000000; i++)  ;

    ab=55;

    cout << "\n----------------\n";

    for(j=0; j<100; j++) 
        cout << endl << ab; 

    return 0;    
}

The purpose of this code is also the same but what was expected from this code is the value of ab becomes 55 after some delay and before that the 2nd pragma block should print 10 and then 55 (multithreading) , but the second pragma block prints only after the delay created by the first for loop and then prints only 55.

#include <iostream>
#include <omp.h>
using namespace std;

int main()
{
    long j, i; 
    int ab=10;
    omp_set_num_threads(2);
    #pragma omp parallel
    {   
        #pragma omp single
        {
            for(i=0; i<1000000000; i++)   ; 
            ab=55;
        }

        #pragma omp barrier

        cout << "\n----------------\n";

        #pragma omp single
        {
            for(j=0; j<100; j++)
             cout << endl << ab; 
        }
    }
    return 0;
}
varun
  • 23
  • 5
  • Does that for loop really delay the program? Any decent compiler would remove that out completely. And [don't use `endl` unnecessarily](http://stackoverflow.com/q/213907/995714) – phuclv Nov 21 '16 at 11:17

1 Answers1

0

So you want to "observe race conditions" by changing the value of a variable in a first region and printing the value from the second region.

There are a couple of things that prevent you achieving this.

  1. The first (and explicitly stated) is the #pragma omp barrier. This OpenMP statement requests the runtime that threads running the #pragma omp parallel must wait until all threads in the team arrive. This first barrier forces the two threads to be at the barrier, thus at that point ab will have value 55.

  2. The #pragma omp single (and here stated implicitly) contains an implicit `` waitclause, so the team of threads running theparallel region` will wait until this region has finished. Again, this means that ab will have value 55 after the first region has finished.

In order to try to achieve (and note the "try" because that will depend from run to run, depending on several factors [OS thread scheduling, OpenMP thread scheduling, HW resources available...]). You can give a try to this alternative version from yours:

#include <iostream>
#include <omp.h>
using namespace std;

int main()
{
    long j, i;
    int ab=10;
    omp_set_num_threads(2);
    #pragma omp parallel
    {
        #pragma omp single nowait
        {
            for(i=0; i<1000000000; i++)   ;
            ab=55;
        }

        cout << "\n----------------\n";

        #pragma omp single
        {
           for(j=0; j<100; j++)
              cout << endl << ab;
        }
    }
    return 0;
}

BTW, rather than iterating for a long trip-count in your loops, you could use calls such as sleep/usleep.

Harald
  • 3,110
  • 1
  • 24
  • 35