次の方法で共有


single

コードのセクションをシングル スレッドで実行するように指定します。このとき、必ずしもマスタ スレッドである必要はありません。

#pragma omp single [clauses] 
{
      code_block 
}

パラメータ

  • clause (省略可能)
    0 個以上の句。single によってサポートされている句の一覧については、「解説」を参照してください。

解説

single ディレクティブは次の OpenMP 句をサポートします。

master ディレクティブを使用して、コードのセクションがマスタ スレッドでのみ実行されるように指定できます。

詳細については、「2.4.3 single コンストラクト」を参照してください。

使用例

// 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");
    }
}

read input
compute results
compute results
write output

参照

概念

OpenMP ディレクティブ