0

I was building a console application which acts as a client. It connects to the given ip address of server. I am using Raknet in it. One of the problems i noticed is that the program Consoleapplicationxx.exe is taking high CPU usage (as shown by the Task Manager). 45% and more.

So i examined by code and found Raknet works in a while loop. Following is my code (not exact. from my memory)

int main(int argc, char** argv)
{
    while (1)
    {
        for (packet->Receive(); packet; packet->Deallocate(), packet->Receive()
        {
            switch (packet->data[0])
            {
            case xx:
                break;
            case yy:
                break;
                    ...
            }
        }
    }
    //some stuff and end
}

So i created another console application. Below is the code

#include <iostream>
#include <windows.h>
int main()
{
    while (1)
    {

    }
}

When i run this program, it also uses high cpu. Then i added a Sleep(1000) inside the while loop and cpu usage reduced to 0%. i gradually reduced to sleep to 1 ms

#include <iostream>
#include <windows.h>
int main()
{
    while (1)
    {
        Sleep(1);
    }
}

And it works perfect(0% cpu usage). (Note: I tried putting Sleep(0) and it again high CPU usage 50%) So is this the way to solve this issue? So that i will add Sleep(1) to my original program?

I am planning to distribute the program after it is finished. So it is important to solve the issue.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
habi
  • 125
  • 8
  • Please provide a [mre] of your usage of the library. Typically libraries like this have a function that will block/sleep until new data is available, so you don't have to manually add any sleep. If the amount of data is so large that the program requires 50% CPU, then a sleep won't help either. The second code snippet (and maybe the third as well) has undefined behavior, since an infinite loop in C++ which doesn't do any atomic/volatile/IO operations is not allowed. – user17732522 Feb 25 '22 at 10:52
  • 3
    the while(1) { ... } section, basically tells the computer to loop over the code forever - as theres nothing to execute, it begins the next iteration of the loop - the cpu usage you're seeing is 1 core of the cpu thrashing itself looping... Adding a sleep will permit the core to do something else during the programs 'nap time'. If it has nothing to do, you'll see low utilisation. In summary, you're burning a core doing nothing else but loop. – FreudianSlip Feb 25 '22 at 10:58
  • Yes, adding a sleep is the way to stop burning CPU unnecessarily. If your network is waiting to receive data then there is no point in retrying it the very next microsecond - you may as well wait at least a whole millisecond, given that the general latency of network communications is often tens of milliseconds. – Galik Feb 25 '22 at 11:40
  • If `Receive` function is non-blocking you will be peaking to the data buffer with extremely high frequency most of the time discovering that there's nothing in. *If* so, then sleeping reduces CPU consumption, but the price you pay for is some (potential) delay if then suddenly data arrives. Explicitly waiting for data (`select` – Windows: on sockets only –, `WaitForSingleObject`, `WaitForMultipleObjects`, `WSAWaitForMultipleEvents`) avoids this problem and additionally unnecessary wake-ups (when sleeping duration elapses), but is slightly more complex to implement. – Aconcagua Feb 25 '22 at 12:45
  • Thank you everyone. I will implement sleep for some milliseconds. – habi Feb 25 '22 at 13:30
  • 1
    Using `sleep` to poll is not very good way to do anything. You should call some function, which returns only when there is something to do (like when nerwork data can be read). – hyde Jul 03 '22 at 16:06

0 Answers0