Question

What's the detail about gdb, does it hold one thread when debug code? I set a exit flag in main thread, and I have joined other threads before print the flag. When I run the debug edition using gdb after compling with argument "-g",it looks :

[Thread debugging using libthread_db enabled]
Input number of threads:5

Main thread id: 0xb7fda6c0, 
[New Thread 0xb7fd9b70 (LWP 9276)]
[New Thread 0xb75d8b70 (LWP 9277)]
[New Thread 0xb6bd7b70 (LWP 9278)]
[New Thread 0xb61d6b70 (LWP 9279)]
[New Thread 0xb57d5b70 (LWP 9280)]
I am thread 0xb6bd7b70, myorder 2, thread_exit.
I am thread 0xb61d6b70, myorder 3, thread_exit.
I am thread 0xb7fd9b70, myorder 0, thread_exit.
I am thread 0xb57d5b70, myorder 4, thread_exit.
I am thread 0xb75d8b70, myorder 1, thread_exit.
[Thread 0xb61d6b70 (LWP 9279) exited]
[Thread 0xb6bd7b70 (LWP 9278) exited]
[Thread 0xb75d8b70 (LWP 9277) exited]
[Thread 0xb7fd9b70 (LWP 9276) exited]
Main: completed join with thread 0xb7fd9b70 having a status of 0
Main: completed join with thread 0xb75d8b70 having a status of 1
Main: completed join with thread 0xb6bd7b70 having a status of 2
Main: completed join with thread 0xb61d6b70 having a status of 3
Main: completed join with thread 0xb57d5b70 having a status of 4
Main: lock will be destroy
Main: 9273, tlist will be free 
Main exit.
[Thread 0xb57d5b70 (LWP 9280) exited]

Program exited normally.
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686 libgcc-4.4.4-13.el6.i686

GDB have give many information about "create" and "exit" of threads. But there is always one thread exited after calling pthread_join() and printing "Main exit" in main thread. Why? Is the thread which gdb work with? Run the release edition, nothing special:

Input number of threads:5

Main thread id: 0xb77176c0, 
I am thread 0xb5913b70, myorder 3, thread_exit.
I am thread 0xb4f12b70, myorder 4, thread_exit.
I am thread 0xb6314b70, myorder 2, thread_exit.
I am thread 0xb6d15b70, myorder 1, thread_exit.
I am thread 0xb7716b70, myorder 0, thread_exit.
Main: completed join with thread 0xb7716b70 having a status of 0
Main: completed join with thread 0xb6d15b70 having a status of 1
Main: completed join with thread 0xb6314b70 having a status of 2
Main: completed join with thread 0xb5913b70 having a status of 3
Main: completed join with thread 0xb4f12b70 having a status of 4
Main: lock will be destroy
Main: tdata list will be free 
Main exit.
Was it helpful?

Solution

gdb, does it hold one thread when debug code?

No.

there is always one thread exited after calling pthread_join() and printing "Main exit" in main thread. Why?

The notification that glibc sends to GDB to tell it that a thread has exited is sent as (nearly) the last thing a thread does. In particular, that notification is sent after the exiting thread has notified the main thread that it has exited (i.e. after the exiting thread wakes the main thread). Therefore, it's not at all surprising that main thread wakes up and prints Main exit before GDB notices that the exiting thread has in fact exited.

I have run it many times, there is only one thread after "Main exit" for each time.

That proves nothing. If you run your program 1,000,000 times, there will probably be at least one run where GDB will notice the thread exiting before Main exit is printed. The ordering is likely dependent on how many processors your system has, and how busy they are. There is really nothing (interesting) to see here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top