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

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

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

调试代码示例:

#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 @ 2024-01-16 13:51  TechNomad  阅读(147)  评论(0)    收藏  举报