You are right to use a loop, but:
- Do you have an IO
- You slept
In principle, nothing in this cycle will take a lot of processor time compared to the time it sleeps or waits for I / O.
To kill a CPU, you just need to give it a processor. The only tricky bit really is that the C ++ compiler does not optimize the loop. Perhaps something like this should be fine:
Note that I am returning a value from a loop. This should stop the C ++ compiler, noticing that the loop is useless (if you had never used a value anywhere, the loop would have no purpose). You can also disable inlining, since otherwise I would suggest that the smart compiler would notice that you are calling the function without using the return value, and inserting it into nothing. (As Suma points out in the answer, using volatile when calling a function should disable inlining.)
source share