GDB调试之多线程调试管理(十四)

一、线程管理相关命令的介绍

  • info threads:查看所有线程信息
  • thread find:查找线程
  • thread num:切换线程
  • thread name:设置线程名字
  • b breakpoint thread id:为线程设置断点
  • thread apply:为线程执行命令
  • set scheduler-locking off|on|step:锁定线程

调试代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstring>
#include <thread>
using namespace std;
class test
{
public:
    test(){}
    virtual ~test(){}
public:
    static void do_work_1()
    {
        cout << "do work 1" << endl;
        std::this_thread::sleep_for(std::chrono::seconds(2));
        cout << "thread do work 1 exited" << endl;
    }
    void do_work_2()
    {
        cout << "do work 2" << endl;
        std::this_thread::sleep_for(std::chrono::seconds(2));
        cout << "thread do work 2 exited" << endl;
    }
    void do_work_3(void *arg,int x,int y)
    {
        char *data = (char*)arg;
        cout << "do work 3:" << data << ",x=" << x << ",y=" << y << endl;
        std::this_thread::sleep_for(std::chrono::seconds(2));
        cout << "thread do work 3 exited" << endl;
    }
};
void test_thread(void *data)
{
    int *val = (int*)data;
    cout << "thread data:" << *val << endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    cout << "test thread exited" << endl;
}
int main(int argc,char** argv)
{
    int data=10;
    thread t1(&test_thread,(void*)&data);
 
    thread t2(&test::do_work_1);   
     
    test test3;
    thread t3(&test::do_work_2,test3);
     
    test test4;
    thread t4(&test::do_work_3,test4,(void*)"test",10,20);
     
    test test5;
    thread t5(&test::do_work_3,test5,(void*)"test2",30,40);
 
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    cout << "threads exit" << endl;
    return 0;
}

 二、线程管理命令的使用 

查看所有线程信息:

切换线程:

查找线程:

设置线程名字:

为线程设置断点:

线程函数中设置普通断点:

为指定线程设置断点:

为线程执行命令:

控制线程日志信息:

 
posted @   TechNomad  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示