《软件调试的艺术》学习笔记——GDB使用技巧摘要(4)——Pthread线程调试

本文是关于《软件调试的艺术》的学习笔记,重点介绍了如何使用GDB进行Pthreads线程调试。通过GDB命令如`info threads`, `thread n`, `break line_num thread n`等来定位和解决线程间可能出现的死锁问题。在遇到死锁时,可以中断程序,查看各线程状态,利用`backtrace`深入分析,并关注关键函数以找出问题代码。" 81066550,5092705,PCM音频采样率转换实现,"['音频开发', '音视频处理', '车联网技术', '通信算法', '开源库']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unix下最普遍的线程包是POSIX标准的Pthreads。Pthreads使用的抢占式线程管理策略,程序中的一个线程可能在任何时候被另一个线程中断。所以,使用Pthreads开发的应用程序有些错误不太容易重现。

GDB线程相关命令汇总

  • info threads 给出关于当前所有线程的信息
  • thread n 改为线程n,或者说是进入线程n的栈中进行观察
  • break line_num thread n 表示当线程n到达源码行line_num时停止执行
  • break line_num thread n if expression 上一命令增加条件断点而已

加入怀疑线程之间有死锁,可以用gdb进行调试定位。流程大致是:

  1. 用gdb启动或者插入待调试程序
  2. 当程序挂起时候,通过按下Ctrl+C组合键中断它;
  3. 这个时候用info threads查看所有线程都在干嘛,然后找到自己的工作线程(注意排除main线程和pthreads的管理线程)
  4. 分别查看自己的工作线程在干嘛,用bt(backtrace)查看对应的帧,记得用thread n切换进入对应线程的帧
  5. 关注像__pthread_wait_for_restart_signal()和lock等函数,如果有源码的话,会比较方便地定位到具体的问题代码位置

下面是一个简单的例子。如果在worker线程里面,上锁和解锁没有匹配,则会发生死锁

// finds the primes between 2 and n; uses the Sieve of Eratosthenes,
// deleting all multiples of 2, all multiples of 3, all multiples of 5,
// etc.; not efficient, e.g. each thread should do deleting for a whole
// block of values of base before going to nextbase for more 

// usage:  sieve nthreads n
// where nthreads is the number of worker threads

#include      <stdio.h>
#include      <math.h>
#include      <pthread.h>

#define MAX_N     100000000
#define MAX_THREADS  100

// shared variables
int nthreads,  // number of threads (not counting main())
    n,  // upper bound of range in which to find primes
    prime[MAX_N+1],  // in the end, prime[i] = 1 if i prime, else 0
    nextbase;  // next sieve multiplier to be used

int work[MAX_THREADS];  // to measure how much work each thread does,
                        // in terms of number of sieve multipliers checked

// lock index for the shared variable nextbase
pthread_mutex_t nextbaselock = PTHREAD_MUTEX_INITIALIZER;

// ID structs for the threads
pthread_t id[MAX_THREADS];

// "crosses out" all multiples of k, from k*k on
void crossout(int k)
{  int i;

   for (i = k; i*k <= n; i++)  {
      prime[i*k] = 0;
   }
}

// worker thread routine
void *worker(int tn)  // tn is the thread number (0,1,...)
{  int lim,base;

   // no need to check multipliers bigger than sqrt(n)
   lim = sqrt(n);

   do  {
      // get next sieve multiplier, avoiding duplication across threads
      pthread_mutex_lock(&nextbaselock);
      base = nextbase += 2; 
      pthread_mutex_unlock(&nextbaselock); 

         if (base <= lim)  {
         work[tn]++;  // log work done by this thread
         // don't bother with crossing out if base is known to be
         // composite
         if (prime[base])
            crossout(base);
      }
      else return;
   } while (1);
}

main(int argc, char **argv)
{  int nprimes,  // number of primes found 
       totwork,  // number of base values checked
       i;
   void *p;

   n = atoi(argv[1]);
   nthreads = atoi(argv[2]);
   for (i = 2; i <= n; i++)
      prime[i] = 1;
   crossout(2);
   nextbase = 1;
   // get threads started
   for (i = 0; i < nthreads; i++)  {
      pthread_create(&id[i],NULL,(void *) worker,(void *) i);
   }

   // wait for all done
   totwork = 0;
   for (i = 0; i < nthreads; i++)  {
      pthread_join(id[i],&p);
      printf(" %d
           values of base done
          /n
          ",work[i]);
      totwork += work[i];
   }
   printf("
          %d 
          total values of base done
          /n
          ",totwork);

   // report results
   nprimes = 0;
   for (i = 2; i <= n; i++)
      if (prime[i]) nprimes++;
   printf("the number of primes found was %d
          /n
          ",nprimes);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值