Suppose I have an undirected graph.
A small portion of the graph :
A -----\
C
B -----/
Now the node A and B proceeds to modify parallely node C. // Node A and Node B process Node C in parallel thread.
I want the thread for Node B will wait for thread for Node A to complete processing. It has to be this way. No other way possible.
I also don't want other thread that are processing other Nodes to wait for the above condition, ie, I don't want to use mutex.
I have created a class member boolean flag and want to use that as a condition. // let that flag be d_flag
MT here uses multiple CPUs.
Solution that I think doesn't works :
CPU1 : Thread 1
while (d_flag) {
// busy waiting
}
d_flag = true;
// Critical Section
d_flag = false;
CPU2 : Thread 1
while (d_flag) {
// busy waiting
}
d_flag = true;
// Critical Section
d_flag = false;
My understanding is that if in multiple CPU, Node A thread is running on CPU1 and Node B thread on CPU 2, the spin lock is defeated.
There may be a condition where both the while loop evaluates to false as the test in the while loop is no longer atomic.
Is my understanding correct????? If yes, is there is a solution without using overall mutex.