title: Understanding hardware / OS level thread CPU throttling in iOS
body: <p>I am trying to understand under what circumstances and how iOS may throttle my application threads due to excessive CPU consumption.  The results I'm getting are kind of strange.</p>
  
  <p>I have an application with OpenGL / GLKViewController rendering a view and a separate logic thread, started in the background using NSThread.detachNewThreadSelector, performing calculations.  I find that if I (for purposes of discussion) let my computation thread run flat out as fast as it can, iOS quickly throttles it down. e.g. I monitor the FPS of both the view and my thread and I see that the view maintains e.g. 60fps and my logic thread is humming along but then suddenly drops after a few seconds.</p>
  
  <p>So that makes sense to me that perhaps iOS tries to limit thread consumption.  What is weird is that it doesn't just slow down gradually but it seems to "quantize" my logic thread's FPS at approximately some multiple of the GPU frame rate (i.e. 30 or 60fps)!</p>
  
  <p>Now, keep in mind that there is <em>no synchronization</em> between these threads and the logic loop is self contained hard loop equivalent to <code>while(true)</code> so I have no idea how it's even possible for iOS to accomplish this magic unless it is somehow aware of my top level loop and interjecting itself into it.</p>
  
  <p>In case you don't believe me that there is no synchronization point I will tell you that I have created a test case that literally just has an empty GLKViewController loop and an dumb logic thread that churns some numbers and it exhibits the behavior. Screenshots are below and I can post the code if anyone is interested.</p>
  
  <p>The screenshots below are for two different "loads" of the logic thread, printed at intervals of a second, running on an iPad Air with iOS 8.</p>
  
  <p>What's even stranger is - sometimes setting a lower preferred GLK frame rate (e.g. 30fps) can actually make my logic thread run <em>slower</em>.  I'd have expected that reducing the work done by the GPU would free up (resources / heat dissipation) and reduce the need for throttling, but it doesn't always seem to be the case.</p>
  
  <p>Does anyone have an explanation for this behavior?  Is it documented?  Thanks.</p>
  
  <p><a href="https://i.stack.imgur.com/GGWV4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GGWV4.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/4Sqiv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Sqiv.png" alt="enter image description here"></a></p>
  
  <p>EDIT: My only guess at this point is that if the GPU runs to hot they shut down the second core and migrate threads back to the first... and then somehow thread prioritization accounts for the implicit synchronization, although I still can't envision exactly how this happens.</p>