【发布时间】:2023-01-20 20:57:49
【问题描述】:
我用 c++ 写了一些并尝试在 raspberry pi 上执行它。我注意到 CPU 负载为 100% 然后我从代码中一点一点地删除,以查看导致高负载的原因。现在我的代码看起来像下面的代码(剥离了所有功能)并且它仍然有 99-100% 的负载。 有人能指出我正确的方向吗?
#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;
}
我试图剥离所有功能的代码以得到一个应该具有很少 cpu 负载的基本程序
【问题讨论】:
-
while (true) {}将消耗 100% 的单个 CPU 内核。 -
也许减慢你的 while 循环迭代?您可以让主线程休眠几毫秒。
-
while (true) {} 会将程序置于无限循环中。最初的程序是打开一个 DBC(矢量 CAN 定义文件)并解析它。在 while 循环中,我正在监视 CAN 总线消息并计算它们。正如描述中所说,我逐行删除了代码,直到我只剩下这个基本框架,但仍然找不到导致高负载的原因。
-
while (true) {}导致高负载 -
如果你取出
while (true) {},高负载应该会消失。
标签: c++ load cpu infinite-loop