This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/6868] stack dirtied after pthread_create


------- Additional Comments From spcurry5 at linux dot vnet dot ibm dot com  2008-09-05 01:27 -------
Hello Luca,

First off, I believe you meant for the first argument to the pthread_create call
(on line 20) to be "&threads[i]", not "&threads[10]". &threads[10] is indexing
beyond the array of "pthread_t". This typo is why you said that "'i' gets
dirtied after the first pthread_create call". 

If you fix that problem and run your test case repeatedly, you will find that
sometimes the output is correct, with the correct 'i' values (0-9) being
printed, but sometimes invalid values are printed by the threads. You may think
this is caused by an undefined "i" value, but it is actually because the scope
of the "array[10]" is local to the main function. ie: when the main
function/thread ends, any variables with local scope will become undefined, and
cannot be depended upon. So, sometimes all of the 10 threads are completing
their printf calls while main's local variables are still valid, and sometimes not. 

Defining the "i" variable outside the main function does not fix the problem,
because the threads are attempting to print the values from the "array[10]"
variable, not from the "i" variable directly.

To see that this is a variable scope problem (and not a problem with
pthread_create), you could declare "array[10]" outside the main function (thus
giving the array a global scope), or you could create a barrier using
"pthread_join", so that the main thread does not exit until all threads have
completed (see attachment a2.c).

I hope this helps you to understand what Jakob is saying.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |spcurry5 at linux dot vnet
                   |                            |dot ibm dot com


http://sourceware.org/bugzilla/show_bug.cgi?id=6868

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]