目录
1.调试代码
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
struct thread_param
{
char info;
int num;
};
void* thread_fun(void* param)
{
struct thread_param* p;
p = (struct thread_param*)param;
int i;
printf("thread pid : %d ,tid:%lu\n",getpid(),pthread_self());
for(i=0;i<p->num;i++){
#if 0/*change to #if 1 for debugging hign cpu-loading issues*/
while(1);
#else
sleep(1);
#if 0 /* change to #if 1 for debugging core dump*/
if(i == 10){
volatile *p = 0;
*p = 0;
}
#endif
#endif
printf("%i : %c\n",i,p->info);
}
return NULL;
}
int main()
{
pthread_t tid1,tid2;
struct thread_param info1;
struct thread_param info2;
int ret;
info1.info='T';
info1.num=2000000;
printf("main pid:%d,tid:%lu\n",thread_fun,&info1);
ret = pthread_create(&tid1,NULL,thread_fun,&info1);
if(ret == -1){
perror("cannot create new thread.");
return 1;
}
info2.info='S';
info2.num=2000000;
ret = pthread_create(&tid2,NULL,thread_fun,&info2);
if(ret == -1){
perror("cannot create new thread.");
return 1;
}
if(pthread_join(tid1,NULL)!=0){
perror("pthread_join function fail");
return 1;
}
if(pthread_join(tid2,NULL)!=0){
perror("pthread_join function fail");
return 1;
}
return 0;
}
2.调试过程与涉及到的命令
3.非常好的一篇文章
4.线程的相关命令
thread apply all command 所有的线程都执行后面的命令(command为GDB的命令)
thread apply all bt 查看所有线程堆栈
thread apply ID1,ID2,ID3.... command 指定哪些线程执行后面的命令(command为GDB的命令)
set non-stop on/off 当调试一个线程时,其它线程是否运行
show scheduler-locking 查看当前锁定线程的模式
董哥的黑板报