LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 09-08-2010, 01:38 AM   #1
Harris_777
LQ Newbie
 
Registered: Sep 2010
Location: India
Posts: 3

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


Hi all,
#include<stdio.h>
#include<pthread.h>

program
**********
pthread_mutex_t mutex1;
int shared_data = 0;

void *sharedata( void *threadid)
{

int i;
for(i= 0; i<1024*1024 ;i++)
{
pthread_mutex_lock(&mutex1);
shared_data++;
pthread_mutex_unlock(&mutex1);
}
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++)
{
//sleep(1);
pthread_mutex_lock(&mutex1);
printf("\nShared data is %d", shared_data);
pthread_mutex_unlock(&mutex1);
}

pthread_join(tid,&exitstat);
return 0;
}


output
******

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
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
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
Harris_777
LQ Newbie
 
Registered: Sep 2010
Location: India
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks Mara!!
 
  


Reply


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 12:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration