Hi,
here is the example code:
device void waitFlag(int *flag){
int val = atomicCAS(flag,0,1);
if(val==1){
do{
val = atomicAdd(flag,0)
}while (val==1);
}
else{
atomicExch(flag,2);
}
}
Image this case, thread A and thread B are in the same wrap,
At the very beginning, the flag is set to 0,
Then both thread A and thread B entry into function waitFlag.
At the atomicCAS, we assume thread A the set *flag success, so after that the val in thread A is 0, and in thread B is 1.
As thread A and thread B are in the same wrap, so we assume thread B execute the follow code first, so thread B is active, and thread A is divergent.
While thread B will in the do…while loop until flag is set to 2,
But only thread A can set the flag to 2, and thread A is divergent.
So the dead lock happen.
Am I right?
looking forward to any reply.
Thanks.