void dequeue ()
{
QITEM *qKill = qHead;
.
.
.
#pragma omp critical
qHead = qHead->qNext;
free(qKill);
}
void enqueue (int iNode, int iDist, int iPrev)
{
.
.
.
QITEM *qLast = qHead;
#pragma omp critical
{
while (qLast->qNext) qLast = qLast->qNext;
qLast->qNext = qNew;
}
}
I know that if only one thread can access the critical section in enqueue and dequeue.
However, if one thread is accessing the critical section in enqueue and the other thread is accessing the critical section in dequeue then is this code protecging the shared list properly?
qHead is a pointer to the head of the linked list.
In your code, you are using unnamed critical section in OpenMP, which is the global and unique critical section. So, only a single thread can enter the section of the critical sections in your code. To answer your question, yes you don’t need to worry about the mutual exclusiveness as there is a single lock.
In order to use multiple locks in OpenMP, please use a name in
criticalpragma.http://software.intel.com/en-us/articles/more-work-sharing-with-openmp/
An example would be
#pragma omp critical(maxvalue).However, it is obvious that having multiple locks will increase the possibility of dead locks, data races, lock convoying, and any kinds of concurrency bugs.