title: How do I abruptly stop a thread currently executing in the background?
body: <p><strong>FACTS:</strong></p>
  
  <p>I have a method executing on a background thread :</p>
  
  <pre><code>[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
  </code></pre>
  
  <p>This method basically contains a while loop that keeps on executing until the user clicks on a <em>Quit</em> button:</p>
  
  <pre><code>-(void) playOn: (UIViewController*) thisViewController
  {
      while(!quitButtonPressed)
      {
         // this is my method's main loop
      }
  }
  </code></pre>
  
  <p><strong>PROBLEM:</strong></p>
  
  <blockquote>
    <p>If the user clicks on Quit somewhere in the middle of the above loop
    <em>the rest</em> of the loop would have to execute <em>before</em> it checks the BOOL once again and eventually stops. In order to prevent that from
    happening and have the while-loop stop <em>as soon as</em> the user clicks on
    Quit, I guess I could also add <em>many</em> <code>if(quitButtonPressed) break;</code>
    <em>here and there</em> in my while loop in order to <em>semi-constantly</em> check and <em>"immediately"</em> break away if needed. However, this doesn't seem
    very clever or practical from a design perspective given the <em>size</em> of
    the above main while-loop and the fact that it contains <em>many smaller
    while-loops</em> inside of it (the number of <code>if.. break;</code> I would have to
    add would be quite big and could make things quite complicated to
    figure out..)</p>
  </blockquote>
  
  <p><strong>POSSIBLE SOLUTION</strong> (but is it the right one?) <strong>:</strong></p>
  
  <blockquote>
    <p>So I was thinking that the best way would be to stop/cancel the
    background thread on which the above method's while loop is executing,
    instead of the while-loop itself, inside the method, the moment the
    user clicks on <em>Quit</em></p>
    
    <p>Is this, or something similar (i.e. a better suggestion), possible and
    how exactly could I do this?</p>
  </blockquote>
  
  <p><strong>POSSIBLE IMPLEMENTATION OF ABOVE SOLUTION:</strong></p>
  
  <p>I could create this new method:</p>
  
  <pre><code>-(void)checkQuitButton
  {
     while(!quitButtonPressed)
     {
        //wait
     }
     if(quitButtonPressed)
     {
       // stop this thread--&gt;[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
       // this is the method I'm looking for
     }
  }
  </code></pre>
  
  <p>And then I could start executing the above and my previous main method concurrently on two separate background threads as follows:</p>
  
  <pre><code>[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
  [currentGame performSelectorInBackground:@selector(checkQuitButton) withObject:nil];
  </code></pre>
  
  <p>While the <em>game</em> while-loop is being executed <em>another</em> while-loop is checking the QuitButton at the same time. But is there a method that I can actually call in order to cancel what was started here:</p>
  
  <pre><code>[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
  </code></pre>
  
  <p>?</p>