top -H是以多线程的方式来展示
pstack pid能够展示某个进程的堆栈
#include<stdio.h>//perror在此.h
#include<iostream>
#include<pthread.h>
#include<unistd.h>//sleep在此.h
using namespace std;
void * routine(void *arg)
{
int i = int(*(int *)arg);
cout << "this is thread " << i << endl;
while(1)
{
}
}
int main()
{
for(int i = 0; i < 5; i++)
{
pthread_attr_t *attr = NULL;
pthread_t t;
int r = pthread_create(&t, attr, routine, &i);
if(r != 0)
{
perror("error!");
}
}
while(1)
{
sleep(1);//保持主线程一直在,这样能防止子线程编程僵尸线程
}
return 0;
}
编译选项: g++ -o test test.cpp -lpthread
输出结果:
this is thread 5
this is thread 5
this is thread 5
this is thread 5
this is thread 5
存在问题:不是自己想要得到的,发生了什么,明天继续来看。