I'm trying to implement the compare and swap operation so that my threads know whether or not to enter a particular region based on a value u_parent
. I just want to know if this is the correct way to implement it and if I am going wrong somewhere.
int *u_parent;
u_parent = &graph->parents[u];
if (*u_parent < 0) {
// This region should only be entered by each thread
// if the value of u_parent is < -1.
graph->parents[u] = v;
// ^-- If region is entered, this value is modified
// by some variable v
//Swap:
u_parent = &graph->parents[u];
DO_SOMETHING();
}
Is this implementation correct because I am still seeing other threads enter this region after the CAS operation?