一、线程的查看
0.首先创建两个线程:
- 分析:下面的程序中创建了两个线程,程序执行起来,main函数所在程序为主线程,在这个主线程中有两个新线程运行
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
void* pthread_run1(void* arg)
{
(void)arg;
while(1)
{
printf("I am thread1,ID: %d\n",pthread_self());
sleep(1);
}
}
void* pthread_run2(void* arg)
{
(void)arg;
while(1)
{
printf("I am thread2,ID: %d\n",pthread_self());
sleep(1);
}
}
int main()
{
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1,NULL,pthread_run1,NULL);
pthread_create(&tid2,NULL,pthread_run2,NULL);
printf("I am main thread\n");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
1.查看当前运行的进程:ps aux|grep a.out
2.查看当前运行的轻量级进程(线程):ps -aL|grep a.out
3.查看主线程和新线程的关系:pstree -p 主线程id

二、进程栈结构的查看:pstack 线程ID

三、利用gdb查看线程信息
1.将进程附加到gdb调试器当中,查看是否创建了新线程:gdb attach 主线程ID

2.查看进程:info inferiors
3.查看线程:info threads
4.查看线程栈结构:bt
5.切换线程:thread n

四、利用gdb调试多线程
1.设置断点与查看断点: break 行号、函数名和info b

2.继续让某一线程运行:thread apply 1-n(第几个线程) n

3.只运行当前线程:set scheduler-locking on

4.所有线程并发执行:set scheduler-locking off
