title: Limit CPU usage in conjunction with Parallel.ForEach
body: <h2>What i need :</h2>
  
  <p>I need to run an array of methods in multithreading with maximum 1 thread per core.   Each of these thread need to have a throttling feature to be able to limit CPU usage of all threads. If i specify 10% on 4 core running 3 threads (because i choose 3 was the max) all 3 threads need to not take more than 10% CPU usage per core. Something exactly how some application like Steam where you can limit the download speed but imagine you would have multiple internet connection to represent the multi core.</p>
  
  <h2>What i tried :</h2>
  
  <p>I have tried using <code>Task</code> but i couldn't figure out how to manage the maximum amount of threads it create and even less which core they each uses.</p>
  
  <h2>What i end up having :</h2>
  
  <p>I ended up finding <code>Parallel.ForEach</code> with combinaison of <code>ParallelOptions.MaxDegreeOfParallelism</code> gives me exactly the control that i want over the amount of threads. I know how much cores i have and i can easily make something to let the user pick the amount of core to use. My test concluded that <code>Parallel.ForEach</code> attribute work on each core in a sequence and once all core have received something it loop back and reassign again. Which is perfect. If i set parallelism of 3 on a 4 core i get 3 thread on 3 different core, while setting parallelism of 5 on a 4 core then 3 core will have 1 thread and 1 core will have 2. That behavior is exactly what i need. This method is not set in stone i am open to changes.</p>
  
  <h2>CPU limitation</h2>
  
  <p>Now using the <code>Parallel.ForEach</code> make the cpu work 100% of the time which is a big problem in that tight environment where any usage over 40-50% over a long period of time over many computers increase the wattage drawn so much that it trigger a critical alarm in the building and we need to be below that. Anyhow i have not found any solution other than doing a thread.sleep in the tight loop but that still make the cpu run 100% for couple seconds then nearly 0% for longer period and then it does that over and over again. This did not work. I ran a small test in one of the room with barely 150 computers and half of them spiked at 100% at the nearly the same exact time drawing WAY too much power. My second test was to use only the 8 cores or more and run on 1 core only and that manage to reduce the usage by a lot.</p>
  
  <h2>Priority</h2>
  
  <p>I have finally tested with Process priority and it's not better. Yes the process receive way less CPU time but the cpu still run at 100% when i does get power. Unless i have miss a critical option when dealing with the priority that can limit the CPU.</p>
  
  <h2>Finally</h2>
  
  <p>I have the feeling i am very close with <code>Parallel.ForEach</code> i am just missing the CPU limitation per core. Am i missing just 1 thing ? I know that using <code>Thread.Sleep</code> is completely stupid as it actually limit the time the cpu is used and not the amount.</p>
  
  <h2>For those who want code</h2>
  
  <p>again this is prototype so the code is pretty much useless. All it does it run code on many cpu until it finishes</p>
  
  <pre><code>static void Main(string[] args)
  {
      var items = Enumerable.Range(0, int.MaxValue - 100);
  
      Parallel.ForEach(
               items,
               new ParallelOptions { MaxDegreeOfParallelism = 4 },
               value =&gt; { Calculate(value); }
       );
  }
  
  private static void Calculate(int value)
  {
      System.Threading.Thread.SpinWait(100000);
      var test = value;
      test *= -1;
      Console.WriteLine(test);
  }
  </code></pre>