我想通過控制何時執行哪些線程來調試多線程程序。我正在使用C++和gdb。除了主線程(例如程序)外,我還有兩個線程,我想調試一個線程,同時保持另一個線程停止。如何將gdb調試一次限制爲一個線程
下面是例子程序我寫道:
#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#define NUM_THREADS 2
using namespace std;
void * run (void *) {
for (int i = 0; i < 3; ++i) {
sleep(1);
cout << i << " " << pthread_self() << endl;
}
pthread_exit(NULL);
}
int main (int argc, char** argv) {
cout << "Start..." << endl;
int rc;
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; ++i) {
rc = pthread_create(&threads[i], NULL, run, NULL);
if (rc) {
cout << "pthread_create returned error: " << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}
我運行GDB,並設置在線路斷點與sleep(1)
。然後我運行該程序。我得到三個線程(線程2和3是pthreads),程序在線程2(等待在sleep(1)
)。現在,我想保持線程3在任何地方,並且繼續通過線程2(通過在gdb中執行c
)。
我試過的是set scheduler-locking on
,但它似乎沒有像我預期的那樣工作。我在線程2,我set scheduler-locking on
,continue
幾次(到目前爲止好,我仍然在線程2),切換到線程3,set scheduler-locking on
,continue
,由於某種原因,我回到線程2 ...當我不應該(根據我的理解)。有什麼我失蹤?
感謝您的鏈接......事實證明,即使當我在線程3中設置了scheduler-locking on之後的'next'時,它仍然將控制權轉移回線程2.(如果我'步驟',但是很難逐步完成庫代碼中的每條指令。)除了'set scheduler-locking on'之外,有沒有其他的命令可以實現這個技巧? –
似乎沒有更好的方法,並且你澄清了這個問題。所以,接受這個答案。 –
使用'set scheduler-locking on'被認爲是OS依賴的。似乎在我的系統上工作正常(Linux 4.4.0內核,gdb 7.11.1,Ubuntu 16.04) –