I really am not sure what you try to achieve here... Actually, without any sort of work done by other threads outside of the single block, I don't see the point of the structure.
Anyway, I tried to extrapolate a bit and extended your example by adding a printf() statement outside of the block which also prints the value of a to see how this is transmitted to the other threads. Moreover, since you used a single directive, I assumed you wanted only one thread executing the block, even if it is on a while() loop. So it looked very well suited for the use of OpenMP locks...
Here is what I came up with:
#include <stdio.h>
#include <omp.h>
#include <unistd.h>
int main() {
int a = 10;
bool finished = false;
omp_lock_t lock;
omp_init_lock( &lock );
#pragma omp parallel num_threads( 3 ) shared( a, finished )
{
while( !finished ) {
if ( omp_test_lock( &lock ) ) {
printf( "[%d] a is: %d\n", omp_get_thread_num(), a );
#pragma omp atomic update
a--;
usleep( 10 );
finished = true;
#pragma omp flush( finished )
omp_unset_lock( &lock );
}
#pragma omp flush( finished, a )
printf( "[%d] outside of if block, a is: %d\n", omp_get_thread_num(), a );
}
}
return 0;
}
I've added a call to usleep() to delay a bit the execution of the instructions inside the if block and give the other threads the opportunity to print something. I've tested it with gcc version 4.9 and 5.3 and Intel compiler 16.0, and all 3 give me the same sort of output, (with obviously some variations in the order and number of printings between runs).
The results looks like this:
~/tmp$ icpc -fopenmp omplock.cc
~/tmp$ ./a.out
[0] a is: 10
[1] outside of if block, a is: 10
[1] outside of if block, a is: 9
[1] outside of if block, a is: 9
[1] outside of if block, a is: 9
[1] outside of if block, a is: 9
[1] outside of if block, a is: 9
[2] outside of if block, a is: 10
[1] outside of if block, a is: 9
[0] outside of if block, a is: 9
Would this approach address your needs?