Using lock in OpenMP and Pthreads
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:
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);
开发者_Go百科 // Do some work
}
}
Thank you.
Depending on the OpenMP implementation, the underlying code may well use pthreads. That said, the OpenMP specification says nothing about whether or not different threading models will "play nicely together". This may or may not work depending on whether the implementation you are using has done the work to allow it to. Unfortunately, all I can say, is to check the documentation for the product you are using and see if it says anything. I believe most implementations have tried to allow this to work.
Use atomic operations to change count. First, using a mutex to protect a simple ++ or -- is unnecessary. Mutexes are for protecting anything that cannot be done atomically any other way. Second, in my mind, performance = 1/((locks)^5). I.e. locks rapidly become the source of performance problems in threaded apps, so avoid them. Third...the atomic op will play nicely with OpenMP. Use __sync_add_and_fetch or a similar atomic operation to change count. Its implemented in hardware on the chip, so its only about ~2x slower than ++ or --, compared to about 40x slower when using mutexes.
精彩评论