This is the mail archive of the libc-help@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] |
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] http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_exit.3.html -- Bharath
#include <pthread.h> #include <stdio.h> #include <stdlib.h> void *printhello(void *arg) { int *pa; pa = (int *) arg; printf("Hello world: %d\n", *pa); pthread_exit(NULL); } int main(int argc, char *argv[]) { int a; pthread_t thread1, thread2; a = 42; pthread_create(&thread1, NULL, printhello, (void *) &a); pthread_create(&thread2, NULL, printhello, (void *) &a); pthread_create(&thread2, NULL, printhello, (void *) &a); pthread_create(&thread2, NULL, printhello, (void *) &a); pthread_exit(NULL); return EXIT_SUCCESS; }
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |