// didattica.cpp : definisce il punto di ingresso dell'applicazione console.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{


#ifdef _OPENMP

	cout << " Number of Processors: "<< omp_get_num_procs()<<endl;
	omp_set_num_threads(omp_get_num_procs());
#endif
#ifdef _OPENMP
int a=-1;
int flag=1;
int count=0;
#pragma omp parallel  num_threads(2)
	{ int TID;	
	TID=omp_get_thread_num();
#pragma omp sections 
	{



#pragma omp section /////////// Producer
		{
		for(int i=0; i<9;i++) 
			{
			a=i;
#pragma omp flush(a)
			flag=1;
			printf("Producer a: %d  flag:%d  TID %d \n",a,flag,TID);
while(flag)
{	
#pragma omp flush(flag)	
}
				}
		flag=2;
#pragma omp flush(flag)
		}


		#pragma omp section  /////////// Consumer
		{
while(1) {
	count++;
#pragma omp flush(a)			
		printf("Consumer a: %d  Flag: %d  count %d TID %d \n",a,flag,count,TID);
			flag=0;
			while(!flag)
				{
#pragma omp flush(flag)
				}
if (flag==2) break;
			
			}
		} /// end consumer

}// end sections
	
#pragma omp barrier
cout<<"Dopo le section !  a: " << a << " TID: " <<TID <<endl;

	
	}
#else
cout << " Iterazione Numero " << i << endl;
#endif
	
	char cc;
	cin>>cc;
	
	return 0;
}

