Operating System - Linux
1826004 Members
3424 Online
109690 Solutions
New Discussion

Re: Doubt with pthread_exit

 
rymani
Occasional Advisor

Doubt with pthread_exit

Manual page of pthread_create says it will implicitly call pthread_exit when start_routine returns.
Is there way to know whether pthread_exit is called.
In c++ we can have a print statement in destructor function, which shows that destructor is called when an object is getting out.
4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: Question on pthread_exit

>In C++ we can have a print statement in destructor function, which shows that destructor is called when an object is getting out.

There is no connection between destructors and threads.
If you call pthread_exit, the destructors for any local objects will not be called. (At least on IPF.)

Since you are returning from the start function that already calls the destructors for any locals.

If you want to know if pthread_exit is called, put a breakpoint there.

rymani
Occasional Advisor

Re: Doubt with pthread_exit

>If you want to know if pthread_exit is called, put a breakpoint there.

I'm not using an explicit call to pthread_exit, so i think i can't able to put a breakpoint.

>If you call pthread_exit, the destructors for any local objects will not be called.

If there is no explicit call to pthread_exit, will it lead to memory leak.
Dennis Handly
Acclaimed Contributor

Re: Question on pthread_exit

>I'm not using an explicit call to pthread_exit, so I think I can't able to put a breakpoint.

No, you can still set a breakpoint there to see who else calls it. In my case I'm just returning out of the start routine:
(gdb) bt
#0 0x2000000076ef1a30:2 pthread_exit+0x72 /usr/lib/hpux32/libpthread.so.1
#1 0x2000000076ef04c0:0 __pthread_bound_body+0x190 libpthread.so.1

So libpthread automatically calls it.

>If there is no explicit call to pthread_exit, will it lead to memory leak.

No, you may leak memory if you don't return/throw back to your start routine.
Especially if you have locals that are STL containers. See:
http://docs.hp.com/en/10946/threads.htm
Pthreads (Posix Threads) Limitations

rymani
Occasional Advisor

Re: Doubt with pthread_exit

Thanks Dennis, for the valuable suggestions provided.