先写个死循环示例
#include <iostream>
#include <thread>
#include <string>
#include <unistd.h>
int main()
{
std::thread([&]
{
while(true)
{
}
}).detach();
std::thread([=]
{
while(true)
{}
}).detach();
while(true)
{}
std::cout << "Hello world" << std::endl;
return 0;
}
编译g++ main.cpp -g -o test -std=gnu++11 -pthread
运行./test
这时候通过top命令可以看到test进程的cpu占用率异常高
用top -H -p 26647查看各个线程占用的cpu情况
然后我们可以用pstack 26647查看堆栈信息
这样就可以看个各个线程的堆栈信息,具体的代码行也可以清晰看到。