Search code examples
c++loadcpuinfinite-loop

C++ CPU load too high for a very small program


I wrote some in c++ and try to execute it on raspberry pi . I noticed a CPU Load of 100 % I then removed bit for bit from the code to see what causes the high load. Now my code looks like the code below (stripped of all functionality ) and it still has 99-100% load. Can someone point me in the right direction ?

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <map>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <cmath>
#include <sys/socket.h>
#include <arpa/inet.h>

using namespace std;

int main(int argc, char* argv[])
{
    // Check command line arguments
    if (argc < 3) {
        cout << "Usage: Test can_name dbc_file" << endl;
        return 1;
    }

// Get can name and dbc file name from command line arguments
    string canName = argv[1];
    string dbcFileName = argv[2];

    while (true) {

    }
    return 0;
}

I tried to strip my code of all funtionality to end up with a basic program that should have very little cpu load


Solution

  • Try adding a sleep within the loop like

    while ( true ) {
        usleep(100000); // sleeps for 100 ms
    }