Hi,
I have a program that uses both Pthreads and OpenMP. Basically, 2 threads (Thread A and B) are created using Pthreads to do work, and in Thread A, OpenMP is used to parallelize a for loop.
If I have a global variable that is accessed by the OpenMP threads and also Thread B, can I use the lock in OpenMP to ensure I have no race conditions?
What I have in mind:
Code:
int count = 0;
pthread_create(&ThreadA, &attr, WorkA, NULL);
pthread_create(&ThreadB, &attr, WorkB, NULL);
void *WorkA (void *t)
{
#pragma omp parallel for
for (i = 0 ; i < N ; i++)
{
// Do some work
#pragma omp critical
{
// Do some other work
OMP_SET_LOCK(&lock);
count++;
OMP_UNSET_LOCK(&lock);
}
}
}
void *WorkB (void *t)
{
if (count > 0)
{
OMP_SET_LOCK(&lock);
count--;
OMP_UNSET_LOCK(&lock);
// Do some work
}
}
Thank you.