title: Cuda Mutex, why deadlock?
body: <p>I am trying to implement a atomic based mutex.</p>
  
  <p>I succeed it but I have one question about warps / deadlock.</p>
  
  <p>This code works well.</p>
  
  <pre><code>bool blocked = true;
  
  while(blocked) {
      if(0 == atomicCAS(&amp;mLock, 0, 1)) {
          index = mSize++;
  
          doCriticJob();
  
          atomicExch(&amp;mLock, 0);
          blocked = false;
      }
  }
  </code></pre>
  
  <p>But this one doesn't...</p>
  
  <pre><code>while(true) {
      if(0 == atomicCAS(&amp;mLock, 0, 1)) {
          index = mSize++;
  
          doCriticJob();
  
          atomicExch(&amp;mLock, 0);
          break;
      }
  }
  </code></pre>
  
  <p>I think it's a position of exiting loop. In the first one, exit happens where the condition is, in the second one it happens in the end of if, so the thread wait for other warps finish loop, but other threads wait the first thread as well... But I think I am wrong, so if you can explain me :).</p>
  
  <p>Thanks !</p>