title: Exiting a NSThread to break out from an infinite loop without cancellation point
body: <p>I am unsure if what I am trying to achieve is possible.</p>
  
  <p>I got a bug while rendering PDF pages into images, it seems that our application is encountering erroneous PDF documents sometime and some rendering are taking literally forever (never exiting), causing our application to be stopped by the user. (The PDF are truly problematic, even the Apple Preview app stops responding when it shows the problematic pages.)</p>
  
  <p>I tried to fix the issue by putting the pdf rendering work in a second thread, while the current thread awaits for the result and try to cancel the NSThread if the rendering timed out.</p>
  
  <pre><code>MAImageRepDrawing* imageRepDrawing = [[MAImageRepDrawing alloc] initWithImageRep:imgRep ...];
  NSThread* thread = [[NSThread alloc] initWithTarget:imageRepDrawing selector:@selector(doDrawImageRep) object:nil];
  [thread start];
  
  double timeOutAt = 0;
  if (IMAGE_REP_RENDERING_TIMEOUT &gt; 0)
      timeOutAt = CFAbsoluteTimeGetCurrent() + (IMAGE_REP_RENDERING_TIMEOUT / 1000.0);
  while (![imageRepDrawing isEnded]) {
      sleep(1);
      if ((IMAGE_REP_RENDERING_TIMEOUT &gt; 0) &amp;&amp; (CFAbsoluteTimeGetCurrent() &gt; timeOutAt)) {
          [thread cancel];
          break;
      }
  }
  
  [thread release];
  ...
  </code></pre>
  
  <p>Unfortunately, it does not completely fix the problem, it seems that the timeout mechanism is working well and the function returns, and thus the application continue. But the NSThread does not cancel and the application gained an unuseful thread using about 100% of CPU (one core). The next time a timeout occurs, there may be two thread using about 200% of CPU (two cores). Etc. So my conclusion is that the drawing process is not looking for cancellation.</p>
  
  <p>I was looking some kind of fix, even if it may be ugly, like invoking <code>[NSThread exit]</code>, but I can't invoke this static function from the current thread... I don't see any other useful function:
  <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/" rel="nofollow">https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/</a></p>
  
  <p>Is there anything else that could be done?</p>
  
  <p>Thanks!</p>