title: Performance of running a multithreaded program on a single core by setting affinity?
body: <p>In short: </p>
  
  <p>Under what scenarios can running a multithreaded app on a single core destroy performance? </p>
  
  <p>What about setting the affinity of a multithreaded app to only use one core?</p>
  
  <p>In long:</p>
  
  <p>I'm trying to run the physics of a 2D engine on it's own thread. It works, and at first performance seemed normal, but I decided to tell the game to try and run at 10K FPS and the physics at 120FPS, went into Task Manager and set the affinity to where the program could only use one core.</p>
  
  <p>The FPS was at ~1700 before setting the affinity to one core, afterwards it went to ~70FPS. I didn't expect that kind of decrease. I told the game to try and run at 300 FPS and physics at 60FPS. </p>
  
  <p>Same thing happened.</p>
  
  <p>I didn't give it much thought, so I just continued modifying the engine. I tested it again later after changing some of the drawing code, 300 FPS, 60FPS for physics. With all cores allowed it managed 300FPS just fine, with affinity to a single core FPS dropped down to 4. Now I know it can't possibly be that bad running a multithreaded app on a single core or am I ignorant of something that happens when you set the affinity to a single core.</p>
  
  <p>This is about how the rendering/physics runs...</p>
  
  <p>Loop starts</p>
  
  <p>Gather input until (1.0 / FPS) has passed.</p>
  
  <p>Call update.</p>
  
  <p>Lock physics thread mutex because things in the game will be using the physics data and I don't want the engine updating anything until everything in this update call finishes.</p>
  
  <p>Update everything in the game which may send a Draw function object(Holds what to draw, where to draw, how to draw) to the Render queue.</p>
  
  <p>Unlock mutex.</p>
  
  <p>Renderer calls operator() on each function object and removes them from queue.</p>
  
  <p>update screen.</p>
  
  <p>repeat loop.</p>
  
  <p>Physics thread loop:</p>
  
  <pre><code>    ALLEGRO_TIMER* timer(al_create_timer(1.0f / 60.0f));
      double prevCount(0);
  
      al_start_timer(timer);
      while(true)
      {
          auto_mutex lock(m_mutex);
  
          if(m_shutdown)
              break;
          if (!m_allowedToStep)
              continue;
                      // Don't run too fast. This isn't final, just simple test code.
          if (!(al_get_timer_count(timer) &gt; prevCount))
              continue;
  
          prevCount = al_get_timer_count(timer);
  
          m_world-&gt;Step(1.0f / 60.0f, 10, 10); 
          m_world-&gt;ClearForces();
  
      }
  </code></pre>
  
  <p>// Note: Auto mutex is just a really simple object I created to lock a mutex in constructor and unlock it in destructor. I'm using Allegro 5's threading functions.</p>