![42cc7e7fdbd4ea28c75bba16aebb806a.png](https://img-blog.csdnimg.cn/img_convert/42cc7e7fdbd4ea28c75bba16aebb806a.png)
在Linux开发中经常会与多线程打交道,所以多线程开发与调试就很重要
下边说下Linux调试过程中CPU占用率过高的情况怎么调试
CPU占用过高,模拟CPU占用过高的情况
先上一段代码:
#include
用下边的命令编译( linux标准库没有pthread.h 要链接之-lpthread):
g++ -o allen_test allen_test.cpp -lpthread
执行可执行文件:
./allen_test
由代码可知,线程中没有进行睡眠,会独占进程的时间片,导致CPU利用率过高,现在开始定位
第一步:使用top命令查看程序进程id
![d57a211732956788808ae69c61c629d6.png](https://img-blog.csdnimg.cn/img_convert/d57a211732956788808ae69c61c629d6.png)
第二步:top -H -p 33401 定位CPU占用过高的线程id
![58e9c0c922ea57f3ded3a7649e0e63ad.png](https://img-blog.csdnimg.cn/img_convert/58e9c0c922ea57f3ded3a7649e0e63ad.png)
第三步:strace -p33411定位线程堆栈
![e50ee888d336cb8122f8855d73270fad.png](https://img-blog.csdnimg.cn/img_convert/e50ee888d336cb8122f8855d73270fad.png)
----------------------------------------------------------------------------------------
2020年7月21日 晚
我的首发平台是微信公号【CodeAllen】,喜欢的小伙伴欢迎关注并回复“1024”获取资料