OpenMP lock booking
#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;
}