중요 한
코드입니다만 실행 하도록 스레드를 한 번에 지정 됩니다.
#pragma omp critical [(name)]
{
code_block
}
설명
다음은 각 매개 변수에 대한 설명입니다.
- (name) (optional)
중요 한 코드를 식별 하는 이름입니다.Note이 이름을 괄호로 묶어야 합니다.
설명
중요 한 지시문 OpenMP 절을 지원 합니다.
자세한 내용은 2.6.2 중요 한 구문를 참조하십시오.
예제
// omp_critical.cpp
// compile with: /openmp
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int main()
{
int i;
int max;
int a[SIZE];
for (i = 0; i < SIZE; i++)
{
a[i] = rand();
printf_s("%d\n", a[i]);
}
max = a[0];
#pragma omp parallel for num_threads(4)
for (i = 1; i < SIZE; i++)
{
if (a[i] > max)
{
#pragma omp critical
{
// compare a[i] and max again because max
// could have been changed by another thread after
// the comparison outside the critical section
if (a[i] > max)
max = a[i];
}
}
}
printf_s("max = %d\n", max);
}