How to limit CPU usage within C++ (Linux)

Discussion in 'C++' started by Alexander Dong Back Kim, Aug 19, 2008.

  1. Dear members,

    First of all, I apologize that this question is not 100% pure C++
    question so some might be annoyed by this.

    I'm writing a multithreading based application. The application can
    use flexible number of threads while it's running (between 2~32
    threads when it's needed or configured). My development machine has 8
    CPU so the application I'm writing sometimes uses 600% of CPU usage
    which was very impressive at the beginning. However, since the real
    target machine has only 1 CPU and requires to run many other
    applications at the same time, I think I should somehow limit the CPU
    usage of my application (let say 30%.

    I tried to use sleep() but I found out it's not good enough because I
    want to explicitly define maximum CPU usage rate for this application.
    Any suggestion or recommendation are welcomed and appreciated.

    regards,
     
    Alexander Dong Back Kim, Aug 19, 2008
    #1
    1. Advertising

  2. Alexander Dong Back Kim

    peter koch Guest

    On 19 Aug., 10:44, Alexander Dong Back Kim <>
    wrote:
    > Dear members,
    >
    > First of all, I apologize that this question is not 100% pure C++
    > question so some might be annoyed by this.
    >
    > I'm writing a multithreading based application. The application can
    > use flexible number of threads while it's running (between 2~32
    > threads when it's needed or configured). My development machine has 8
    > CPU so the application I'm writing sometimes uses 600% of CPU usage
    > which was very impressive at the beginning. However, since the real
    > target machine has only 1 CPU and requires to run many other
    > applications at the same time, I think I should somehow limit the CPU
    > usage of my application (let say 30%.
    >
    > I tried to use sleep() but I found out it's not good enough because I
    > want to explicitly define maximum CPU usage rate for this application.
    > Any suggestion or recommendation are welcomed and appreciated.
    >
    > regards,


    There is no good C++ answer to your question - using sleep is
    certainly not a good solution.
    The way to go is to have "nice" threads - giving them a lower priority
    than the normal user threads. This way you can get all the CPU-time
    when noone else is using the CPU(s) without impeding them they want to
    run something "more important".

    /Peter
     
    peter koch, Aug 19, 2008
    #2
    1. Advertising

  3. Alexander Dong Back Kim

    Lionel B Guest

    Re: [OT] How to limit CPU usage within C++ (Linux)

    On Tue, 19 Aug 2008 01:44:48 -0700, Alexander Dong Back Kim wrote:

    > Dear members,
    >
    > First of all, I apologize that this question is not 100% pure C++
    > question so some might be annoyed by this.
    >
    > I'm writing a multithreading based application. The application can use
    > flexible number of threads while it's running (between 2~32 threads when
    > it's needed or configured). My development machine has 8 CPU so the
    > application I'm writing sometimes uses 600% of CPU usage which was very
    > impressive at the beginning. However, since the real target machine has
    > only 1 CPU and requires to run many other applications at the same time,
    > I think I should somehow limit the CPU usage of my application (let say
    > 30%.
    >
    > I tried to use sleep() but I found out it's not good enough because I
    > want to explicitly define maximum CPU usage rate for this application.
    > Any suggestion or recommendation are welcomed and appreciated.


    man nice

    --
    Lionel B
     
    Lionel B, Aug 19, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mmf

    cpu usage limit

    mmf, May 27, 2005, in forum: Python
    Replies:
    9
    Views:
    881
  2. Terry Reedy

    Re: How to limit CPU usage in Python

    Terry Reedy, Sep 20, 2012, in forum: Python
    Replies:
    0
    Views:
    165
    Terry Reedy
    Sep 20, 2012
  3. Terry Reedy

    Re: How to limit CPU usage in Python

    Terry Reedy, Sep 20, 2012, in forum: Python
    Replies:
    0
    Views:
    165
    Terry Reedy
    Sep 20, 2012
  4. Jerry Hill

    Re: How to limit CPU usage in Python

    Jerry Hill, Sep 20, 2012, in forum: Python
    Replies:
    0
    Views:
    184
    Jerry Hill
    Sep 20, 2012
  5. Cameron Simpson

    Re: How to limit CPU usage in Python

    Cameron Simpson, Sep 22, 2012, in forum: Python
    Replies:
    2
    Views:
    278
    Ramchandra Apte
    Sep 22, 2012
Loading...

Share This Page