Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-08-2010, 01:38 AM   #1
LQ Newbie
Registered: Sep 2010
Location: India
Posts: 3

Rep: Reputation: 0
problem in uderstanding pthread execution in given code

Hi all,

pthread_mutex_t mutex1;
int shared_data = 0;

void *sharedata( void *threadid)

int i;
for(i= 0; i<1024*1024 ;i++)
printf("\nIn shared function%d",shared_data);

int main()

int tid;
void *exitstat;
int t;
int i;

pthread_mutex_init(&mutex1, NULL);

pthread_create(&tid, NULL, sharedata,(void *)&t);

for(i= 0; i<10; i++)
printf("\nShared data is %d", shared_data);

return 0;


I am getting different outputs in main() when I run the same program.Please explain why?
I also want to know at what point the switching takes place between shareddata() and main() function.
Old 09-08-2010, 02:23 PM   #2
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,545

Rep: Reputation: 160Reputation: 160
Your output is perfectly normal on Linux. The switch moment is generally independent from your code, just like typical process switches. On some implementations of pthread, however, your code matters because the switches are done on system calls and on pthread* functions (in your case it means printf and pthread*).
1 members found this post helpful.
Old 09-08-2010, 11:07 PM   #3
LQ Newbie
Registered: Sep 2010
Location: India
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks Mara!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
pthread source code girish_hilage Linux - Software 3 10-12-2007 12:58 PM
Pthread Problem IBall Programming 2 04-21-2006 02:05 AM
problem in understanding step by step execution of n-code files Najia Iqbal Programming 5 04-06-2006 12:55 AM
Flash Player Unspecified Code Execution Vulnerabilities (Highly Critical) win32sux Linux - Security 1 03-14-2006 09:35 PM

All times are GMT -5. The time now is 02:24 AM.

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