flush
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

flush (OpenMP) 

Specifies that all threads have the same view of memory for all shared objects.


#pragma omp flush [(var)]

where,

var (optional)

A comma-separated list of variables that represent objects you want to synchronize. If var is not specified, all memory is flushed.

The flush directive supports no OpenMP clauses.

For more information, see 2.6.5 flush Directive.

// omp_flush.cpp
// compile with: /openmp 
#include <stdio.h>
#include <omp.h>

void read(int *data) {
   printf_s("read data\n");
   *data = 1;
}

void process(int *data) {
   printf_s("process data\n");
   (*data)++;
}

int main() {
   int data;
   int flag;

   flag = 0;

   #pragma omp parallel sections num_threads(2)
   {
      #pragma omp section
      {
         printf_s("Thread %d: ", omp_get_thread_num( ));
         read(&data);
         #pragma omp flush(data)
         flag = 1;
         #pragma omp flush(flag)
         // Do more work.
      }

      #pragma omp section 
      {
         while (!flag) {
            #pragma omp flush(flag)
         }
         #pragma omp flush(data)
       
         printf_s("Thread %d: ", omp_get_thread_num( ));
         process(&data);
         printf_s("data = %d\n", data);
      }
   }
}

Sample Output

Thread 0: read data
Thread 1: process data
data = 2
Show:
© 2016 Microsoft