c_cpp OpenMP锁定预订

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp OpenMP锁定预订相关的知识,希望对你有一定的参考价值。

#include <assert.h>
#include <libiomp/omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>


const int M = 3, N = 9;
int seats[M][N];
int passenger = 4;
omp_lock_t writeLock;

int main(void) {
  omp_init_lock(&writeLock);

  for (int x = 0; x < M; x++) {
    for (int y = 0; y < N ; y++ ) {
      if ((x + y) % 2 == 0) {
        seats[x][y] = 1;
      } else {
        seats[x][y] = 0;
      }
    }
  }

#pragma omp parallel for
  for (int p = 0; p < passenger; p++) {
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
        sleep(rand() % 5);
        omp_set_lock(&writeLock);
        if (seats[i][j] == 0) {
          sleep(rand() % 5);
          seats[i][j] = 1;
          printf("Passenger %d, book seat, [%d, %d]\n", p, i, j);
        }
        omp_unset_lock(&writeLock);
      }
    }
  }
  return 0;
}

以上是关于c_cpp OpenMP锁定预订的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp openmp.c

OpenMP:锁定对单个数组元素的访问?

c ++ OpenMP关键:“单向”锁定?

c_cpp 144.二叉树预订遍历

c_cpp [HackerRank]预订Hackathon 2015

c_cpp 启用BUILD_WITH_OPENMP时如何解决高CPU使用率问题