Reputation: 1040
How i can limit cpu usage to 10% for example for specific process in windows C++?
Upvotes: 3
Views: 2558
Reputation: 9508
This is rarely needed and maybe thread priorities are better solution but since you asked, what you should is:
twork
millisecondsSleep()
for (100/percent - 1)*twork
milliseconds where percent
is your desired loadIn order for this to work well, you have to be really careful in selecting how big a "fraction" of a calculation is and some tasks are hard to split up. A single fraction should take somewhere between 40 and 250 milliseconds or so, if it takes less, the overhead from sleeping and measuring might become significant, if it's more, the illusion of using 10% CPU will disappear and it will seem like your thread is oscillating between 0 and 100% CPU (which is what happens anyway, but if you do it fast enough then it looks like you only take whatever percent). Two additional things to note: first, as mentioned before me, this is on a thread level and not process level; second, your work must be real CPU work, disk/device/network I/O usually involves a lot of waiting and doesn't take as much CPU.
Upvotes: 2
Reputation: 701
You could use Sleep(x)
- will slow down your program execution but it will free up CPU cycles
Where x is the time in milliseconds
Upvotes: 2
Reputation: 1458
You can't limit to exactly 10%, but you can reduce it's priority and restrict it to use only one CPU core.
Upvotes: 0