[SOLVED] Very simple pthread program sometimes prints garbage value for a variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Very simple pthread program sometimes prints garbage value for a variable
Code:
#include <pthread.h>
#include <stdio.h>
void* multi(void* tid)
{
int id = *((int*)tid);
printf("%d\n",id);
}
int main()
{
int a = 1;
pthread_t thread1;
pthread_create(&thread1, NULL, multi, (void*)(&a));
pthread_exit(NULL);
return 0;
}
If you run this program using watch -n 0.1 ./a.out you will occasionally see garbage values being printed. According to the pthread_exit man page, the main function will be alive till the child thread completes. If that's the case, 'a' should still be present on the stack, and I should never get garbage values.
According to the pthread_exit man page, the main function will be alive till the child thread completes. If that's the case, 'a' should still be present on the stack, and I should never get garbage values.
According to the man page, when pthread_exit() is called from the main function, the variable used for the return value (retval) should not be declared on the stack within main, for it will be destroyed when the main thread exits. One has to assume that this same principle applies to other variables within the main function that are also declared on the stack.
If you declare 'a' outside of main, say as a global variable, then your program should function as expected.
I suggest you remove pthread_exit from main, and see if the problem still persists.
If I remove pthread_exit from main, the process will terminate even if the threads do not. I don't want this to happen.
Quote:
According to the man page, when pthread_exit() is called from the main function, the variable used for the return value (retval) should not be declared on the stack within main, for it will be destroyed when the main thread exits. One has to assume that this same principle applies to other variables within the main function that are also declared on the stack.
But the main thread does not exit when you call pthread_exit from within it according to the man page. So my variable should be alive. Please refer to the 'Notes' section: http://linux.die.net/man/3/pthread_exit
I have also tested this case by making the thread perform infinite calculation. The process does not terminate.
Quote:
If you declare 'a' outside of main, say as a global variable, then your program should function as expected.
Yes, I noticed this, but I don't want to be forced to use global variables. It will become unwieldly in large programs.
But the main thread does not exit when you call pthread_exit from within it according to the man page. So my variable should be alive.[/url]
The main thread does exit! And as for the local variables declared on the stack, they are trashed. Try this version of your program; notice that the printf() in main is never executed.
Code:
#include <pthread.h>
#include <stdio.h>
void* thread(void* arg)
{
int id = *((int*) arg);
printf("%d\n", id);
return NULL;
}
int main()
{
pthread_t tid;
int id = 1;
pthread_create(&tid, NULL, thread, &id);
pthread_exit(NULL);
printf("The main thread should never reach this point.\n");
return 0;
}
In examining the bigger picture, why do you want the main function to exit? You could leave it running by having it block on an individual thread using pthread_join().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.