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.
|