当前位置: 技术问答>linux和unix
linux 多线程调试问题,新手,高人指点!!!!!!!!!
来源: 互联网 发布时间:2016-02-11
本文导语: 请问如何用gdb在linux下调试多线程程序 #include #include #include #include "pthread.h" #define BUFFER_SIZE 16 /* zheng shu xunhuan huanchongqi */ struct prodcons { int buffer[BUFFER_SIZE]; pthread_mutex_t lock; int readpos, writepos; pthread_cond_t...
请问如何用gdb在linux下调试多线程程序
#include
#include
#include
#include "pthread.h"
#define BUFFER_SIZE 16
/* zheng shu xunhuan huanchongqi */
struct prodcons
{
int buffer[BUFFER_SIZE];
pthread_mutex_t lock;
int readpos, writepos;
pthread_cond_t notempty;
pthread_cond_t notfull;
};
/* chushihua huanchongqu */
void init(struct prodcons* b)
{
pthread_mutex_init(&b->lock, NULL);
pthread_cond_init(&b->notempty, NULL);
pthread_cond_init(&b->notfull, NULL);
b->readpos = 0;
b->writepos = 0;
}
/*---------------------------------------------------------------------*/
/* zai huanchongqu cunfang yige zhengshu */
void put(struct prodcons* b,int data)
{
pthread_mutex_lock(&b->lock);
/* buffer wei feikong zhiqian chuyu dengdaizhuangtai */
while(( b->writepos + 1) % BUFFER_SIZE == b->readpos)
{
printf("wait for not fulln");
pthread_cond_wait(&b->notfull, &b->lock);
}
/* xie shuju, bing ba xiezhizhen qianyi */
b->buffer[b->writepos] = data;
b->writepos++;
if(b->writepos >= BUFFER_SIZE) b->writepos = 0;
/* tishi huanchongqu buffer xianzai wei kong */
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
/*-----------------------------------------------------------------------*/
/* duqu yige zhengshu bing cong huanchongqu buffer zhong yichu */
int get(struct prodcons * b)
{
int data;
pthread_mutex_lock(&b->lock);
/* buffer wei feikong zhiqian chuyu dengdaizhuangtai */
while (b->writepos == b->readpos)
{
printf("wait for not emptyn");
pthread_cond_wait(&b->notempty, &b->lock);
}
/* duqu shuju bing qianyi duzhizhen */
data = b->buffer[b->readpos];
b->readpos++;
if(b->readpos >= BUFFER_SIZE) b->readpos = 0;
/* tishi huanchongqu buffer xianzai weiman */
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
/*-----------------------------------------------------------------------*/
#define OVER (-1)
struct prodcons buffer;
/*-----------------------------------------------------------------------*/
void * producer(void * data)
{
int n;
for (n = 0; n lim
当你的程序被GDB停住时,所有的运行线程都会被停住。这方便你你查看运行程序的总体情况。而在你恢复程序运行时,所有的线程也会被恢复运行。那怕是主进程在被单步调试时。
========================================
查的资料,可以参考下
#include
#include
#include
#include "pthread.h"
#define BUFFER_SIZE 16
/* zheng shu xunhuan huanchongqi */
struct prodcons
{
int buffer[BUFFER_SIZE];
pthread_mutex_t lock;
int readpos, writepos;
pthread_cond_t notempty;
pthread_cond_t notfull;
};
/* chushihua huanchongqu */
void init(struct prodcons* b)
{
pthread_mutex_init(&b->lock, NULL);
pthread_cond_init(&b->notempty, NULL);
pthread_cond_init(&b->notfull, NULL);
b->readpos = 0;
b->writepos = 0;
}
/*---------------------------------------------------------------------*/
/* zai huanchongqu cunfang yige zhengshu */
void put(struct prodcons* b,int data)
{
pthread_mutex_lock(&b->lock);
/* buffer wei feikong zhiqian chuyu dengdaizhuangtai */
while(( b->writepos + 1) % BUFFER_SIZE == b->readpos)
{
printf("wait for not fulln");
pthread_cond_wait(&b->notfull, &b->lock);
}
/* xie shuju, bing ba xiezhizhen qianyi */
b->buffer[b->writepos] = data;
b->writepos++;
if(b->writepos >= BUFFER_SIZE) b->writepos = 0;
/* tishi huanchongqu buffer xianzai wei kong */
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
/*-----------------------------------------------------------------------*/
/* duqu yige zhengshu bing cong huanchongqu buffer zhong yichu */
int get(struct prodcons * b)
{
int data;
pthread_mutex_lock(&b->lock);
/* buffer wei feikong zhiqian chuyu dengdaizhuangtai */
while (b->writepos == b->readpos)
{
printf("wait for not emptyn");
pthread_cond_wait(&b->notempty, &b->lock);
}
/* duqu shuju bing qianyi duzhizhen */
data = b->buffer[b->readpos];
b->readpos++;
if(b->readpos >= BUFFER_SIZE) b->readpos = 0;
/* tishi huanchongqu buffer xianzai weiman */
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
/*-----------------------------------------------------------------------*/
#define OVER (-1)
struct prodcons buffer;
/*-----------------------------------------------------------------------*/
void * producer(void * data)
{
int n;
for (n = 0; n lim
当你的程序被GDB停住时,所有的运行线程都会被停住。这方便你你查看运行程序的总体情况。而在你恢复程序运行时,所有的线程也会被恢复运行。那怕是主进程在被单步调试时。
========================================
查的资料,可以参考下
|
http://book.77169.org/ask30/how210438.htm
要点是先运行程序,再attach线程号进行调试。
1。先运行你的主程序
2。使用ps查看你程序的线程号,或者在线程程序里打印出来
3。运行gdb,运行“attach 线程号”
4。info stack看运行到哪里了,设线程文件中的断点
5。跟踪调试
6。“detach 线程号”取消调试该线程
要点是先运行程序,再attach线程号进行调试。
1。先运行你的主程序
2。使用ps查看你程序的线程号,或者在线程程序里打印出来
3。运行gdb,运行“attach 线程号”
4。info stack看运行到哪里了,设线程文件中的断点
5。跟踪调试
6。“detach 线程号”取消调试该线程