Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Create thread with >70% CPU utilization

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 0.77 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. while(i++< 2000)
  2. {
  3.    cout<<" in while "<< endl;
  4.    Sleep(10); // sleep for 10 ms.
  5. }
  6.        
  7. // A bit like generating a hashcode. Pretty arbitrary choice,
  8. // but simple code which would be hard for the compiler to
  9. // optimise away.
  10. int running_total = 23;
  11. for (int i=0; i < some_large_number; i++)
  12. {
  13.     running_total = 37 * running_total + i;
  14. }
  15. return running_total;
  16.        
  17. while(i++< 2000)
  18. {
  19.    int sleepBalance = 10; // increase this to reduce the CPU load
  20.    int computeBalance = 1000; // increase this to increase the CPU load
  21.    for (int i=0; i<computeBalance; i++)
  22.    {
  23.      /* both volatiles are important to prevent compiler */
  24.      /* optimizing out the function */
  25.      volatile int n = 30;
  26.      volatile int pretendWeNeedTheResult = Factorial(n);
  27.    }
  28.    Sleep(sleepBalance);
  29. }
clone this paste RAW Paste Data