pthread_exit behavior from main

Bharath Ramesh
Fri Oct 5 00:34:00 GMT 2012

On 10/4/2012 8:30 PM, Mirko Banchi wrote:
> Each thread has its own stack and it's not shared between other threads. This is the reason why you don't see the value 42.
Yes, each thread has its own stack. I am passing the address of a local 
variable in main's thread. Then I de-reference that address in the 
thread function, which should be a valid address in the address space. 
It is not like I am declaring a new local stack variable in the thread 
function. The issue is that some thread do print the value "42" as expected.

> Il giorno 05/ott/2012, alle ore 00:05, Bharath Ramesh <> ha scritto:
>> I am attaching a small code snippet and I am little bit curious
>> on the behavior of pthread_exit from main based on the output. I
>> am passing the address of local stack variable to the threads
>> main is creating. In the thread function I am printing its value.
>> The issue I am noticing is that the value printed isnt what I am
>> expecting it to be "42" for each thread every run. If I run many
>> instances of the application simultaneously some threads report
>> "0" instead of "42".
>> I suspect this behavior is because probably main's stack isnt
>> valid. I agree that it is bad programming practice to pass a
>> local stack variable to the threads spawned. I am confused by the
>> behavior based on what is stated in the pthread_exit manpage [1]
>> in the notes section. The manpage states "To allow other threads
>> to continue execution, the main thread should terminate by
>> calling pthread_exit() rather than exit(3)". Does this imply that
>> main's stack will be valid till all the threads exit?
>> I would really appreciate it if somebody could clarify this
>> behavior I am seeing.
>> [1]
>> -- 
>> Bharath
>> <pthread_exit.c>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3734 bytes
Desc: S/MIME Cryptographic Signature
URL: <>

More information about the Libc-help mailing list