Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
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.

single 

Lets you specify that a section of code should be executed on a single thread, not necessarily the master thread.


#pragma omp single [clauses] 
{
      code_block 
}

Parameters

clause (optional)

Zero or more clauses. See the Remarks section for a list of the clauses supported by single.

The single directive supports the following OpenMP clauses:

The master directive lets you specify that a section of code should be executed only on the master thread.

For more information, see 2.4.3 single Construct.

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

int main() {
   #pragma omp parallel num_threads(2)
   {
      #pragma omp single
      // Only a single thread can read the input.
      printf_s("read input\n");
      
      // Multiple threads in the team compute the results.
      printf_s("compute results\n");

      #pragma omp single
      // Only a single thread can write the output.
      printf_s("write output\n");
    }
}

Sample Output

read input
compute results
compute results
write output

Community Additions

ADD
Show:
© 2015 Microsoft