LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2012, 08:32 AM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
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.
 
Old 05-16-2012, 08:47 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I suggest you remove pthread_exit from main, and see if the problem still persists.

Last edited by NevemTeve; 05-16-2012 at 08:52 AM.
 
Old 05-16-2012, 09:13 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by gregorian View Post
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.
 
Old 05-16-2012, 10:39 AM   #4
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by NevemTeve View Post
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.

Last edited by gregorian; 05-16-2012 at 10:40 AM.
 
Old 05-16-2012, 10:49 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by gregorian View Post
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().
 
1 members found this post helpful.
Old 05-16-2012, 10:57 AM   #6
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Oh I get your point now. Thanks a lot!
 
Old 05-16-2012, 11:22 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> > 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.

Then pthread_join is your friend, not pthread_exit.
 
Old 05-16-2012, 11:30 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
and also there are some OSs where exiting the main thread will destroy all of the threads.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CUPS test page and plain text prints, all else garbage Mig21 Linux - Hardware 2 06-24-2011 01:37 AM
[SOLVED] ncurses screen prints garbage MALDATA Programming 6 02-09-2011 03:10 PM
problem with 'less' command - prints garbage when piped. spoovy Slackware 11 03-09-2010 04:07 AM
Samba shared printer prints garbage gankoji Linux - Server 4 08-02-2009 02:32 PM
IE doesn't load PDF sometimes and prints garbage siucorn Linux - Software 0 08-29-2003 10:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration