User and Reference Guide for the Intel® C++ Compiler 14.0

omp single

Specifies a structured block that will be executed only once by a single thread in the team.

Syntax

#pragma omp single [clause,clause , ...]

structured-block

Arguments

clause

Can be one or more of the following clauses:

  • copyprivate(list)

  • firstprivate(list)

  • nowait(integer expression)

  • private(list)

Description

Only one thread will be allowed to execute the structured block. The rest of the threads in the team wait at the implicit barrier at the end of the single construct, unless nowait is specified. If nowait is specified, then the rest of the threads in the team immediately execute the code after the structured block.

The following example demonstrates how to use this pragma to make sure that the printf function is executed only once. All the threads in the team that do not execute the function proceed immediately to the following calculations.

Example

#include <omp.h>
#pragma omp parallel {
 	#pragma omp single nowait { printf(“Starting calculation\n”); }
	
	// Do some calculation
}

Submit feedback on this help topic