0

I was debugging my app with gdb. I used break main So it can break when main is called. Know if I use thread info it shows that thread count is 1.

How a thread is starting before main ? I don't have any thread call in my call so from where thread is getting created. I am using these libs

sqlite , curl , pcre , c-client

Update I have written a sample program to test that if all program start with single thread

 #include<iostream>
int main(int argc,char *argv[]){
 std:: cout<<"Will I have any thread";
 return 0;
}

but when I debug it with gdb

 (gdb) break main
Breakpoint 1 at 0x400783: file threadtest.cpp, line 3.
(gdb) run
Starting program: /home/vivek/Desktop/a.out 
Breakpoint 1, main (argc=1, argv=0x7fffffffe728) at threadtest.cpp:3
3 std:: cout<<"Will I have any thread";
(gdb) info threads
* 1 process 21608 main (argc=1, argv=0x7fffffffe728) at threadtest.cpp:3
(gdb) 

it doesn't show the same information. It show 1 process not 1 thread.

When I compile it with -lpthread it show 1 thread.

So program start with one thread when we use lpthread ? or GDB behaves like that ?

asked Feb 25, 2011 at 9:56
1
  • 1
    Could it be, that this thread is your main thread? Commented Feb 25, 2011 at 9:59

3 Answers 3

2

All programs have at least 1 thread, the main thread. The program is started before main since the C++ runtime does some initializing before main() starts, like calling all global objects which have constructors.

answered Feb 25, 2011 at 9:58
Sign up to request clarification or add additional context in comments.

2 Comments

When I use -lpthread for linking in sample program gdb show 1 thread. But when not it show one process. So program start with one thread when we use lpthread ? or GDB behaves like that ?
There is always a thread (the main thread, as @Maister mentioned) within a process. But GDB can't tell you anything about threads unless you link with a threading library.
1

The operating system creates a process space with one thread and calls the application loader to execute the application in that thread, which in turns performs some initial setup (gathering command line arguments into argc and argv, for example) and calls main.

answered Feb 25, 2011 at 9:58

Comments

0

For the sample App when I compile it with -lpthread it shows 1 thread is running. So lpthread is playing key point here.

 (gdb) break main
Breakpoint 1 at 0x400793: file threadtest.cpp, line 3.
(gdb) run
Starting program: /home/vivek/Desktop/a.out 
[Thread debugging using libthread_db enabled]
Breakpoint 1, main (argc=1, argv=0x7fffffffe728) at threadtest.cpp:3
3 std:: cout<<"Will I have any thread";
(gdb) info threads
* 1 Thread 0x2aaaaaac8bb0 (LWP 21649) main (argc=1, argv=0x7fffffffe728)
 at threadtest.cpp:3
(gdb) 
answered Feb 25, 2011 at 10:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.