Thread: Controling CPU usage and input reading

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    Controling CPU usage and input reading

    My c++ programs always use the maximum amount of CPU they can get because they are constantly running.

    My question is, how do i make my program w8 for a mouse button event before it continues running??

    Ive googled it and i only found answers related to the windows API, ive worked with it before, but i wanted something more portable.

    How is this done?

    If im not being clear enought pls tell me.

    Tks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you mean the program hogs the CPU when it is not performing a useful activity, that is very bad. However, this is because of some basic design flaw -- the way to fix it is to figure out what the flaw is.

    If you mean you want the program to use less CPU when it is doing something useful, you could add in very short passive delays.

    Generally it is the task of the OS kernel to hand out resources, and optionally the user may have external ways of limiting a process (assigning it a priority, etc). Your focus should be on getting the program to do what it does as optimally as possible, so that will need as few resources as possible. You do not do that by trying to tell the OS to not allocate them.

    Quote Originally Posted by shiroaisu View Post
    My question is, how do i make my program w8 for a mouse button event before it continues running??

    Ive googled it and i only found answers related to the windows API, ive worked with it before, but i wanted something more portable.
    Interaction with the mouse is platform specific, but many cross platform libraries can take care of that for you too. It depends what exactly you are doing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by MK27 View Post
    Interaction with the mouse is platform specific, but many cross platform libraries can take care of that for you too. It depends what exactly you are doing.
    So what your saying is that those kind of things are more API specific right??

    and about the short pauses, i know the windows API has Sleep( ) but what if i want to program for linux for example? Is that API specific too or is there a std sleep i can use?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shiroaisu View Post
    So what your saying is that those kind of things are more API specific right??
    Yes, exactly.

    about the short pauses, i know the windows API has Sleep( ) but what if i want to program for linux for example? Is that API specific too or is there a std sleep i can use?
    Sleep() is a standard C/C++ function. However, it works on the level of seconds, which is quite a big slice of time. Functions for shorter gaps are platform specific, but again, many cross-platform libraries can also deal with this. Eg, it appears boost has something:

    Chapter..1...Boost.Timers

    You might find this useful w/ linux (and the idea is nearly identical on windows):
    SourceForge.net: POSIX timers - cpwiki

    Of course, you cannot use these to "wait to do something". They wait a fixed amount of time, nothing is done, and no resources are consumed. But they cannot be used to wait for something specific to happen.

    Again: this requirement might be indicative of a design flaw, you should be more specific about what you are doing. Hopefully, you do not need to do this at all.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    120
    Tks alot for the quick answers you helped alot man.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you mean the program hogs the CPU when it is not performing a useful activity, that is very bad.
    CPU usage is not an indicator of application performance in all instances. In fact CPU usage is a poor indicator to use for this very thing. CPU usage is simply a rather complex algo that the Windows kernel uses to specify how much of the CPU it feels your app is using in light of other applications. 100% often means you are in a tight loop and not yielding any of your time to any apps but it does not mean the system is going to slow to a crawl. In general it is hard for a normal Windows application to get the usage to 100% but it is very simple for a game to get 1 core's usage to 100% since they are normally inside some tight render/update loop and many of them do not utilize threads very well. Few applications or games max out every core in a multi-core processor b/c the Windows kernel most likely will not allow this to happen.

    There are far more important factors to consider when judging application performance such as memory usage, handle usage, thread counts, etc. than just pure CPU usage. So if you are re-factoring only to get the usage down then that is a poor reason for re-factoring. If you are re-factoring because you have slowed the system down somehow or your app is unreponsive then that is a better reason. If you just want to yield cycles then a simple Sleep() will bring the usage down but I do not recommend this hack just to get the number down.
    Last edited by VirtualAce; 03-27-2011 at 12:42 PM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by VirtualAce View Post
    CPU usage is not an indicator of application performance in all instances. In fact CPU usage is a poor indicator to use for this very thing. CPU usage is simply a rather complex algo that the Windows kernel uses to specify how much of the CPU it feels your app is using in light of other applications.
    That's irrelevant and tangential -- windows may have some application that feeds you some number calculated that way (in which case it is just giving you a misleading number), but that is not what "CPU usage" means in the context of programming.

    It is not an abstract or hard to understand measurement. CPU usage is objective and simple to calculate. Your CPU has a clock rate which is a number of cycles per second. A cycle is a finite event. If the the CPU is working so much that it has no idle cycles during a given time period (eg, one second) then the CPU is working 100%. The kernel knows who does what: if the CPU is maxed out and one process used half those cycles, that processes is using 50% of the CPU time available. Some number representing what "your app is using in light of other applications" is NOT A CORRECT CPU TIME MEASUREMENT.

    Quote Originally Posted by VirtualAce View Post
    In general it is hard for a normal Windows application to get the usage to 100% but it is very simple for a game to get 1 core's usage to 100% since they are normally inside some tight render/update loop and many of them do not utilize threads very well. Few applications or games max out every core in a multi-core processor b/c the Windows kernel most likely will not allow this to happen.
    Wrong. The ONLY reason a single process will not use all the cores is because it is not coded to do so, not because the kernel disallows it. A correctly written multi-threaded program will max all your cores out. If you only have one core, a single non-threaded program can do the same thing. However, most end user desktop applications (such as games) are real time applications with a human being on one end and do not do this because they don't need to -- there is not enough for them to do in parallel. This does not mean it is forbidden/impossible.

    If your operating system will not allow a process to use all of the available CPU time that it wants, you have a bizarre and ass backward operating system. However, if your OS load balances when there are several processes competing for CPU time, and no more is available, that's fine and normal.

    But it should (and in reality, does) allow a single process to hog 100% of the CPU's real cycles if these would otherwise be idle (or very close to it, since the OS kernel itself needs some).

    So yes, in fact it is easy to have one program "hog the CPU" and you can indeed nearly freeze the system. Simple logic: the OS does not know that the process is doing something wrong or irrelevant, and it would be a complete waste of resources if it did not provide fast access to all available resources. 100% of them. Ie, a system which refuses to freeze would be a system which wastes the potential of the hardware by forcing it to remain idle.

    Quote Originally Posted by VirtualAce View Post
    If you just want to yield cycles then a simple Sleep() will bring the usage down but I do not recommend this hack just to get the number down.
    I'll agree with that for the same reason I disagree with your obfuscations about CPU usage and kernel load balancing. The OS deals with these issues efficiently.
    Last edited by MK27; 03-27-2011 at 01:23 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MK27 View Post
    Sleep() is a standard C/C++ function.
    Unless they added Sleep() in C++0x (which I doubt), it's a Windows API function.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And doubt correctly you did!
    (Besides, all STL functions are lower-case, and obviously Sleep isn't.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Wrong. The ONLY reason a single process will not use all the cores is because it is not coded to do so, not because the kernel disallows it. A correctly written multi-threaded program will max all your cores out. If you only have one core, a single non-threaded program can do the same thing. However, most end user desktop applications (such as games) are real time applications with a human being on one end and do not do this because they don't need to -- there is not enough for them to do in parallel. This does not mean it is forbidden/impossible.
    My point being that most games are single core or sometimes dual core optimized at the least. Most retail full-screen OGL or Direct3D games do max out and if you do not believe me then a simple Google for this will show you that people from all over constantly complain about games that are at 50% or above on CPU usage. Games routinely max out the CPU but often only max 1 core out. I would not consider a full screen game a standard desktop application in the strictest sense because there are very few similarities between a normal desktop app and a game. There are far more differences between a full-screen game and a full-screen borderless application than meets the eye. Games, especially some older ones, will definitely max 1 core out on your CPU and yet your system will continue to function just fine.

    ...windows may have some application that feeds you some number calculated that way (in which case it is just giving you a misleading number), but that is not what "CPU usage" means in the context of programming.
    This is what it is doing and since most only use Windows CPU usage as an indicator of true usage then they are using incorrect data to formulate a solution. CPU usage in the context of programming is irrelevant if Windows is simply giving you some other number that it not a true representation of said usage if you are using the Windows CPU usage number as an argument for refactoring your code base.

    My point is that before refactoring any code I would research into what the usage actually means and what it does not mean and possibly some other utilities that break down the usage into more categories and/or meaningful concrete numbers. Just using the task manager to figure out usage stats would be like using the task manager to figure out if you had a memory leak or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong usage of cin.eof()?
    By Memloop in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2009, 06:42 AM
  2. flushall()
    By salvadoravi in forum C Programming
    Replies: 21
    Last Post: 12-24-2007, 12:39 PM
  3. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM