title: How do I limit an external DLL to one CPU?
body: <p>I have a program that I would like to run on just one CPU so it doesn't take up too much system resources. The problem is, it makes a call into an external DLL that automatically uses all available CPU cores. I do not have the source code to the external DLL. How can I limit the DLL to only using one CPU?</p>
  
  <p>EDIT: Thanks for the help, here is the code I used to limit to one CPU (Windows):</p>
  
  <pre><code>// Limit the process to only 1 thread so we don't chew up system resources
  HANDLE ProcessHandle = GetCurrentProcess();
  DWORD ProcessAffinityMask;
  DWORD SystemAffinityMask;
  if(GetProcessAffinityMask(ProcessHandle,&amp;ProcessAffinityMask,&amp;SystemAffinityMask)
      &amp;&amp; SystemAffinityMask != 0)
  {
      // Limit to 1 thread by masking all but 1 bit of the system affinity mask
      DWORD NewProcessAffinityMask = ((SystemAffinityMask-1) ^ SystemAffinityMask) &amp; SystemAffinityMask;
      SetProcessAffinityMask(ProcessHandle,NewProcessAffinityMask);
  }
  </code></pre>
  
  <p>EDIT: Turns out Brannon's approach of setting process priority works even better for what I want, which is to keep the process from chewing up resources. Here's that code (Windows):</p>
  
  <pre><code>// Make the process low priority so we don't chew up system resources
  HANDLE ProcessHandle = GetCurrentProcess();
  SetPriorityClass(ProcessHandle,BELOW_NORMAL_PRIORITY_CLASS);
  </code></pre>