调试-----调试正在运行的多线程程序

 4 using namespace std;
 5 static int a = 1;
 6 
 7 void *thread0(void*) 
 8 { 
 9     while (1)
10     {
11         a++;
12     }
13     return (void*)0;
14 }
15 
16 void *thread1(void*) 
17 { 
18     while (1)
19     {
20         a++;
21     }
22     return (void*)0;
23 }
24 
25 int main()
26 {
27     pthread_t thread[2];
28     pthread_create(&thread[0], NULL, thread0, NULL);
29     pthread_create(&thread[1], NULL, thread1, NULL);
30 
31     for (int i = 0; i < 2; i++)
32     {
33         pthread_join(thread[i], NULL); 
34     }
35     while (1)
36     {
37         sleep(2);
38         cout << "pthread_join is called, block, this not execute" << endl;
39     }
40     
41     return 0;
42 }

main.cpp

1. 运行上面的程序               ./main

2. 查看进程号                     ps aux | grep main

3. attach上去                    gdb  main   [进程号]

4. 显示线程                        info  thread

5. attach上一个线程            thread  3

6. 设断点正常调试(不要输入run运行了, 因为程序已经启动了)

原文地址:https://www.cnblogs.com/helloweworld/p/4050784.html