<p>I was just wondering if there is an elegant way to set the maximum CPU load for a particular thread doing intensive calculations.
<br/><br/>Right now I have located the most time consuming loop in the thread (it does only compression) and use <code>GetTickCount()</code> and <code>Sleep()</code> with hardcoded values. It makes sure that the loop continues for a certain period and then sleeps for a certain minimum time. It more or less does the job, i.e. guarantees that the thread will not use more than 50% of CPU. <br>However, behavior is dependent on the number of CPU cores (huge disadvantage) and simply ugly (smaller disadvantage :)). <br>Any ideas?</p>

[{'answer_id': 1986, 'author': 'sparkes', 'author_id': 269, 'author_profile': 'https://Stackoverflow.com/users/269', 'pm_score': 2, 'selected': False, 'text': '<p>I can\'t think of any cross platform way of what you want (or any guaranteed way full stop) but as you are using GetTickCount perhaps you aren\'t interested in cross platform :)</p>\n\n<p>I\'d use interprocess communications and set the intensive processes nice levels to get what you require but I\'m not sure that\'s appropriate for your situation.</p>\n\n<p>EDIT:\nI agree with <a href="https://stackoverflow.com/questions/1982/#1984">Bernard</a> which is why I think a process rather than a thread might be more appropriate but it just might not suit your purposes.</p>\n'}, {'answer_id': 2006, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 2, 'selected': False, 'text': '<p>On linux, you can change the scheduling priority of a thread with nice().</p>\n'}, {'answer_id': 47086, 'author': 'Matthew Xavier', 'author_id': 4841, 'author_profile': 'https://Stackoverflow.com/users/4841', 'pm_score': 6, 'selected': True, 'text': '<p>I am not aware of any API to do get the OS\'s scheduler to do what you want (even if your thread is idle-priority, if there are no higher-priority ready threads, yours will run).  However, I think you can improvise a fairly elegant throttling function based on what you are already doing. Essentially (I don\'t have a Windows dev machine handy):</p>\n\n<p>Pick a default amount of time the thread will sleep each iteration.  Then, on each iteration (or on every nth iteration, such that the throttling function doesn\'t itself become a significant CPU load),</p>\n\n<ol>\n<li>Compute the amount of CPU time your thread used since the last time your throttling function was called (I\'ll call this dCPU).  You can use the <a href="http://msdn.microsoft.com/en-us/library/ms683237.aspx" rel="noreferrer">GetThreadTimes()</a> API to get the amount of time your thread has been executing.</li>\n<li>Compute the amount of real time elapsed since the last time your throttling function was called (I\'ll call this dClock).</li>\n<li>dCPU / dClock is the percent CPU usage (of one CPU).  If it is higher than you want, increase your sleep time, if lower, decrease the sleep time.</li>\n<li>Have your thread sleep for the computed time.</li>\n</ol>\n\n<p>Depending on how your watchdog computes CPU usage, you might want to use <a href="http://msdn.microsoft.com/en-us/library/ms683213(VS.85).aspx" rel="noreferrer">GetProcessAffinityMask()</a> to find out how many CPUs the system has.  dCPU / (dClock * CPUs) is the percentage of total CPU time available.</p>\n\n<p>You will still have to pick some magic numbers for the initial sleep time and the increment/decrement amount, but I think this algorithm could be tuned to keep a thread running at fairly close to a determined percent of CPU.</p>\n'}, {'answer_id': 47345, 'author': 'Douglas Leeder', 'author_id': 3978, 'author_profile': 'https://Stackoverflow.com/users/3978', 'pm_score': 2, 'selected': False, 'text': "<p>The problem is it's not normal to want to leave the CPU idle while you have work to do. Normally you set a background task to IDLE priority, and let the OS handle scheduling it all the CPU time that isn't used by interactive tasks. </p>\n\n<p>It sound to me like the problem is the watchdog process. </p>\n\n<p>If your background task is CPU-bound then you want it to take all the unused CPU time for its task.</p>\n\n<p>Maybe you should look at fixing the watchdog program?</p>\n"}, {'answer_id': 48082839, 'author': 'Aashishkebab', 'author_id': 9052185, 'author_profile': 'https://Stackoverflow.com/users/9052185', 'pm_score': 0, 'selected': False, 'text': "<p>You may be able to change the priority of a thread, but changing the maximum utilization would either require polling and hacks to limit how many things are occurring, or using OS tools that can set the maximum utilization of a process.\nHowever, I don't see any circumstance where you would want to do this.</p>\n"}]
['https://Stackoverflow.com/questions/1982', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2090742/']